c# - IEnumerable<T >'requires ' 1' 类型参数

标签 c# .net ienumerable postsharp

为了更改提供给我们的 dll,我使用了 IAspectProvider 接口(interface)并满足其所需的 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);
        }
    }
}

运行时出现这些错误

等待您的宝贵意见

最佳答案

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

关于c# - IEnumerable<T >'requires ' 1' 类型参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10897453/

相关文章:

C#使用代码而不是设计器将可点击图像添加到gui

c# - 在 C# 中验证一个字符串是否只包含字母

.net - ADO.NET Entity Framework 中的持久性无知是否意味着我认为的意思?

.net - 如何创建 Azure 数字孪生备份并将其恢复到空 ADT 帐户中?

c# - 对 IEnumerable 的替换、插入、删除操作

c# - 如何使用 Linq 查询和 IEnumerable 获取类属性的值

c# - 正确地在页面之间传递数据

c# - 解决连接字符串问题

.net - XPath:具有子节点的节点具有属性

c# - foreach 如何将对象转换为指定类型?