【发布时间】:2010-01-09 06:15:23
【问题描述】:
我有一个构建委托处理程序集合的实现。
public class DelegateHandler
{
internal delegate object delegateMethod(object args);
public IntPtr PublishAsyncMethod(MethodInfo method, MethodInfo callback)
{
RuntimeMethodHandle rt;
try
{
rt = method.MethodHandle;
delegateMethod dMethod = (delegateMethod)Delegate.CreateDelegate
(typeof(delegateMethod), method.ReflectedType, method, true);
AsyncCallback callBack = (AsyncCallback)Delegate.CreateDelegate
(typeof(AsyncCallback), method.ReflectedType, callback, true);
handlers[rt.Value] = new DelegateStruct(dMethod, callBack);
return rt.Value;
}
catch (System.ArgumentException ArgEx)
{
Console.WriteLine("*****: " + ArgEx.Source);
Console.WriteLine("*****: " + ArgEx.InnerException);
Console.WriteLine("*****: " + ArgEx.Message);
}
return new IntPtr(-1);
}
}
我使用以下方式发布:
ptr = DelegateHandler.Io.PublishAsyncMethod(
this.GetType().GetMethod("InitializeComponents"),
this.GetType().GetMethod("Components_Initialized"));
我创建委托的方法是:
public void InitializeComponents(object args)
{
// do stuff;
}
以及回调方法:
public void Components_Initialized(IAsyncResult iaRes)
{
// do stuff;
}
现在,我也已经查看了this,以了解我可能做错了什么。 CreateDelegate(...) 导致我收到:
*****: mscorlib
*****:
*****: Error binding to target method.
怎么了?这些方法驻留在不同的非静态公共类中。任何帮助将不胜感激。
注意:这些方法将有参数和返回值。据我了解Action 和Action<T>,这不是一个选项。
【问题讨论】:
标签: c# reflection delegates