【问题标题】:Building delegate handler collection构建委托处理程序集合
【发布时间】: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.

怎么了?这些方法驻留在不同的非静态公共类中。任何帮助将不胜感激。

注意:这些方法将有参数和返回值。据我了解ActionAction<T>,这不是一个选项。

【问题讨论】:

    标签: c# reflection delegates


    【解决方案1】:

    有两个问题。

    首先,您向CreateDelegate 传递了错误的参数。由于您要绑定到实例方法,因此您需要传递委托将绑定到的实例,但您传递的是 method.ReflectedType 而不是对声明 InitializeComponentsComponents_Initialized 的类的对象的引用。

    第二,InitializeComponents的签名与委托dMethod的声明不符。委托有一个object 返回类型,而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”的签名是帖子中的疏忽。我一直在简化整个事情,只是忘了展示这一点。但是,这绝对是我需要的。我确信我实际上已经尝试过传递目标对象——尽管如此,我们这次做对了。再次感谢...
    猜你喜欢
    • 2014-04-12
    • 2012-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多