我正在使用 dotnet 2.0
我知道使用 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;
}