IEnumerable'需要“1”类型的参数

本文关键字:类型 参数 需要 IEnumerable | 更新日期: 2023-09-27 18:36:15

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

运行此内容时,我收到这些错误

等待您的宝贵意见

IEnumerable<T>'需要“1”类型的参数

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

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