首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MethodInfo和委托

MethodInfo和委托
EN

Stack Overflow用户
提问于 2010-10-08 04:57:59
回答 1查看 397关注 0票数 1

我使用的是dotnet 2.0

我知道,使用EventInfo值,可以遍历程序集的类型并找到与EventInfo委托定义( EventInfo.EventHandlerType )匹配的所有方法。

有没有一种方法可以在Delegate.CreateDelegate()函数中找出给定的MethodInfo可以分配哪些可用委托,而不必首先遍历所有引用的程序集来查找所有委托定义。

或者我被困在做以下事情上:

代码语言:javascript
复制
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;
}
EN

回答 1

Stack Overflow用户

发布于 2010-10-08 05:03:10

听起来你肯定需要遍历所有的东西。你说你想找到所有可以工作的“可用”委托。接受委托的函数没有任何指向可以传递给它的方法的链接,因此大规模搜索将是找到所有这些方法的唯一方法。

您可以通过只检查具有公共/内部访问的类型来减少搜索所花费的时间。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3885834

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档