Search code examples
c#.netienumerablepostsharp

IEnumerable<T>'requires '1' type arguments


In order to make changes in the dll given to us i used IAspectProvider interface and satisfy its required ProvideAspects method. as

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);
        }
    }
}

while running this i am getting these error

waiting for your valuable comments


Solution

  • If you look at the documentation for ProvideAspects(), you will notice that it returns IEnumerable<AspectInstance>, so that's what you have to use in your code too:

    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);
            }
        }
    }