1

我正在使用 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;
}
4

1 回答 1

0

听起来您确实需要遍历所有内容。你说你想找到所有可以工作的“可用”代表。接受委托的函数没有任何指向可以传递给它的方法的链接,因此大量搜索将是找到所有方法的唯一方法。

您可以通过仅检查具有公共/内部访问权限的类型来减少搜索时间。

于 2010-10-07T21:03:10.087 回答