MethodInfo的和委托 [英] MethodInfo and Delegates
问题描述
我使用DOTNET 2.0
我知道有EventInfo值,可以通过大会的类型和循环查找所有EventInfo委托定义(EventInfo.EventHandlerType)匹配方法
有没有办法找出什么可以代表一个给定的MethodInfo可以在Delegate.CreateDelegate()函数来分配不首先遍历所有引用的程序集找到所有的代表定义。
还是我坚持做了以下内容:
公共BOOL MethodInfoDelegateSearch(MethodInfo的MI){
System.Collections.Generic.List<类型> delegateTypes =新System.Collections.Generic.List<类型>();
的foreach(在AppDomain.CurrentDomain.GetAssemblies大会一())
的foreach(在a.GetTypes T型()){
如果(t.IsSubclassOf(typeof运算(代表)))
delegateTypes.Add(T);
}
的for(int i = 0; I< delegateTypes.Count;我++){
类型t = delegateTypes [I]
/ *
*这里是尝试委托结构相匹配的MethodInfo的
*我可以比较的参数或只是尝试创建委托
* /
尝试 {
Delegate.CreateDelegate(T,MI,真正的);
返回true;
} 抓住 {
}
}
返回false;
}
这听起来确实像你将通过一切需要循环。你说,你要找到所有可用的代表,将工作。它接受委托的功能并不一定可能被传递给它的方法中的任何链接,所以一个大的搜索会找到他们的唯一方法。
您可以通过只检查各类公共/内部访问减少花在搜索的时间。
I am using dotnet 2.0
I know that with an EventInfo value, you can loop through an Assembly's Types and find all the methods that match the EventInfo delegate definition ( EventInfo.EventHandlerType )
Is there a way to find out what available delegates a given MethodInfo can be assigned in the Delegate.CreateDelegate() function without first looping through all the referenced assemblies to find all the Delegate definitions.
Or am I stuck doing the following:
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;
}
It sure sounds like you would need to loop through everything. You say that you want to find all "available" delegates that would work. The function which accepts a delegate doesn't have any links to the methods that could be passed to it, so a large search would be the only way to find them all.
You could reduce the time spent searching by only checking the types with public/internal access.
这篇关于MethodInfo的和委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!