为了更改提供给我们的 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/