2010-10-07 71 views
1

我现在用的dotnet 2.0的MethodInfo和委托

我知道有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; 
} 

回答

0

它肯定听起来像是你将通过一切需要循环。你说你想找到所有可以工作的“可用”代表。接受委托的函数没有任何可传递给它的方法的链接,因此大型搜索将是唯一可以找到它们的唯一方法。

您只需通过公开/内部访问来检查类型,就可以缩短搜索时间。