1
我现在用的dotnet 2.0的MethodInfo和委托
我知道有EventInfo值,可以循环通过大会的类型,找到所有符合EventInfo委托定义(EventInfo.EventHandlerType)
的方法有没有办法找到可以在Delegate.CreateDelegate()函数中分配给定MethodInfo的可用委托,而无需首先遍历所有引用的程序集以查找所有委托定义。
还是我坚持做了以下内容:
public bool MethodInfoDelegateSearch(MethodInfo mi) {
System.Collections.Generic.List<Type> delegateTypes = new System.Collections.Generic.List<Type>();
foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies())
foreach (Type t in a.GetTypes()) {
if (t.IsSubclassOf(typeof(Delegate)))
delegateTypes.Add(t);
}
for (int i = 0; i < delegateTypes.Count; i++) {
Type t = delegateTypes[i];
/*
* here is where to attempt match the delegate structure to the MethodInfo
* I can compare parameters or just attempt to create the delegate
*/
try {
Delegate.CreateDelegate(t, mi, true);
return true;
} catch {
}
}
return false;
}