IEnumerable <t>'需要'1'类型参数</t>

时间:2012-06-05 12:38:16

标签: c# .net ienumerable postsharp

为了对我们提供的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);
        }
    }
}

在运行时我收到这些错误

等待您的宝贵意见

2 个答案:

答案 0 :(得分:4)

如果您查看the documentation for 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);
        }
    }
}

答案 1 :(得分:1)

您必须使用IEnumerable<SomeClass>List<someClass>。 另请查看专门用于此类情况的yield return

相关问题