C# 生成委托处理程序集合

C# 生成委托处理程序集合,c#,reflection,delegates,C#,Reflection,Delegates,我有一个构建委托处理程序集合的实现 public class DelegateHandler { internal delegate object delegateMethod(object args); public IntPtr PublishAsyncMethod(MethodInfo method, MethodInfo callback) { RuntimeMethodH

我有一个构建委托处理程序集合的实现

   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;
}
现在,我也已经看过了,想知道我可能做错了什么。CreateDelegate(…)导致我接收:

*****: mscorlib
*****: 
*****: Error binding to target method.
怎么了?这些方法位于不同的非静态公共类中。任何帮助都将不胜感激

注意:这些方法将具有参数和返回值。据我所知,这不是一个选项。

有两个问题

首先,您向
CreateDelegate
传递的参数不正确。由于要绑定到实例方法,因此需要传递委托将绑定到的实例,但传递的是
方法。ReflectedType
,而不是对类中声明
InitializeComponents
Components\u Initialized
的对象的引用

其次,
InitializeComponents
的签名与delegate
dMethod
的声明不匹配。委托有一个
对象
返回类型,但
InitializeComponents
返回
void

以下方面应起作用:

// return type changed to void to match target.
internal delegate void delegateMethod(object args);

// obj parameter added
public static void PublishAsyncMethod(object obj, MethodInfo method, MethodInfo callback)
{
    delegateMethod dMethod = (delegateMethod)Delegate.CreateDelegate
        (typeof(delegateMethod), obj, method, true);

    AsyncCallback callBack = (AsyncCallback)Delegate.CreateDelegate
         (typeof(AsyncCallback), obj, callback);

}

DelegateHandler.PublishAsyncMethod(
    this, // pass this pointer needed to bind instance methods to delegates.
    this.GetType().GetMethod("InitializeComponents"),
    this.GetType().GetMethod("Components_Initialized"));
有两个问题

首先,您向
CreateDelegate
传递的参数不正确。由于要绑定到实例方法,因此需要传递委托将绑定到的实例,但传递的是
方法。ReflectedType
,而不是对类中声明
InitializeComponents
Components\u Initialized
的对象的引用

其次,
InitializeComponents
的签名与delegate
dMethod
的声明不匹配。委托有一个
对象
返回类型,但
InitializeComponents
返回
void

以下方面应起作用:

// return type changed to void to match target.
internal delegate void delegateMethod(object args);

// obj parameter added
public static void PublishAsyncMethod(object obj, MethodInfo method, MethodInfo callback)
{
    delegateMethod dMethod = (delegateMethod)Delegate.CreateDelegate
        (typeof(delegateMethod), obj, method, true);

    AsyncCallback callBack = (AsyncCallback)Delegate.CreateDelegate
         (typeof(AsyncCallback), obj, callback);

}

DelegateHandler.PublishAsyncMethod(
    this, // pass this pointer needed to bind instance methods to delegates.
    this.GetType().GetMethod("InitializeComponents"),
    this.GetType().GetMethod("Components_Initialized"));

首先,谢谢你的回答。令人惊叹的!!!返回类型为“delegateMethod”的签名是帖子中的一个疏忽。我一直在简化整件事,只是忘了展示。然而,这绝对是我所需要的。我确信我确实试着通过了目标物体——尽管如此,这次我们做对了。再次感谢…首先,谢谢你的回答。令人惊叹的!!!返回类型为“delegateMethod”的签名是帖子中的一个疏忽。我一直在简化整件事,只是忘了展示。然而,这绝对是我所需要的。我确信我确实试着通过了目标物体——尽管如此,这次我们做对了。再次感谢。。。