我使用的是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;
}发布于 2010-10-08 05:03:10
听起来你肯定需要遍历所有的东西。你说你想找到所有可以工作的“可用”委托。接受委托的函数没有任何指向可以传递给它的方法的链接,因此大规模搜索将是找到所有这些方法的唯一方法。
您可以通过只检查具有公共/内部访问的类型来减少搜索所花费的时间。
https://stackoverflow.com/questions/3885834
复制相似问题