1

为了对提供给我们的 dll 进行更改,我使用了 IAspectProvider 接口并满足其所需的 ProvideAspects 方法。作为

public class TraceAspectProvider : IAspectProvider {
    readonly SomeTracingAspect aspectToApply = new SomeTracingAspect();

    public IEnumerable ProvideAspects(object targetElement) {
        Assembly assembly = (Assembly)targetElement;
        List instances = new List();
        foreach (Type type in assembly.GetTypes()) {
            ProcessType(type, instances);
        }
        return instances;
    }
    void ProcessType(Type type, List instances) {
        foreach (MethodInfo targetMethod in type.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly)) {
            instances.Add(new AspectInstance(targetMethod, aspectToApply));
        }
        foreach (Type nestedType in type.GetNestedTypes()) {
            ProcessType(nestedType, instances);
        }
    }
}

运行此程序时,我收到这些错误

等待您的宝贵意见

4

2 回答 2

4

如果您查看的文档ProvideAspects(),您会注意到它返回IEnumerable<AspectInstance>,因此您也必须在代码中使用它:

public class TraceAspectProvider : IAspectProvider {
    readonly SomeTracingAspect aspectToApply = new SomeTracingAspect();

    public IEnumerable<AspectInstance> ProvideAspects(object targetElement) {
        Assembly assembly = (Assembly)targetElement;
        List<AspectInstance> instances = new List<AspectInstance>();
        foreach (Type type in assembly.GetTypes()) {
            ProcessType(type, instances);
        }
        return instances;
    }
    void ProcessType(Type type, List<AspectInstance> instances) {
        foreach (MethodInfo targetMethod in type.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly)) {
            instances.Add(new AspectInstance(targetMethod, aspectToApply));
        }
        foreach (Type nestedType in type.GetNestedTypes()) {
            ProcessType(nestedType, instances);
        }
    }
}
于 2012-06-05T12:48:56.477 回答
1

你必须使用IEnumerable<SomeClass>andList<someClass>为此。还要检查yield return专门为在这种情况下使用而设计的。

于 2012-06-05T12:40:32.903 回答