通过反射创建打开的实例委托 [英] Create open instance delegate via reflection

查看:906
本文介绍了通过反射创建打开的实例委托的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么以下 Delegate.CreateDelegate 会产生运行时ArgumentException?

Why does the following Delegate.CreateDelegate produce a runtime ArgumentException?

static class Module1
{
    public static void Main()
    {
        MyDataObject mdo = new MyDataObject();
        mdo.DoMagic("Hello");
    }

    #region Assembly A
    public class BusinessObject
    {
    }

    public class MyBusinessObject : BusinessObject
    {

        public void DoMagic(string s)
        {
            Console.WriteLine(s);
        }
    }
    #endregion

    #region Assembly B
    public class MyDataObject
    {
        private delegate void DoMagicDel(BusinessObject bo, string s);

        private DoMagicDel delDoMagic;
        public void DoMagic(string s)
        {
            BusinessObject bo = (BusinessObject)Activator.CreateInstance(Type.GetType("Module1+MyBusinessObject"));
            MethodInfo mi = bo.GetType().GetMethod("DoMagic", BindingFlags.Public | BindingFlags.Instance);
            // ArgumentException: Cannot bind to the target method because its signature or security transparency is not compatible with that of the delegate type.
            delDoMagic = (DoMagicDel)Delegate.CreateDelegate(typeof(DoMagicDel), null, mi);
            delDoMagic(bo, s);
        }
    }
    #endregion
}

大会A有一个项目参考大会B,反之亦然。
当我更改第一个参数DoMagicMel以输入MyBusinessObject时,它起作用。但是由于程序集B不知道这种类型的程序集A,这只能在我的简化示例中工作:

Where Assembly A has a project reference to Assembly B but not vice versa. It works when I change first parameter DoMagicMel to type MyBusinessObject. But because assembly B doesn`t know this type of assembly A, this works only in my simplified example:

private delegate void DoMagicDel(MyBusinessObject bo, string s);

有机会得到这个工作吗?

Any chance to get this working?

推荐答案

ArgumentException: Cannot bind to the target method because its signature or security transparency is not compatible with that of the delegate type.

为什么以下Delegate.CreateDelegate产生运行时 ArgumentException

Why does the following Delegate.CreateDelegate produce a runtime ArgumentException?


  • 由于 DoMagicDel 的签名与

  • Because the signature of DoMagicDel does not match the signature of the method described by mi.

有机会得到这个工作吗?

Any chance to get this working?


  • 是的,使用 MakeGenericType 匹配mi的签名:

#region Assembly B
public class MyDataObject
{
    public delegate void DoMagicDel<T1>(T1 arg1, string arg2);
    private static Delegate delDoMagic;
    public void DoMagic(string s)
    {
        var bo = Activator.CreateInstance("Module1", "Module1.MyBusinessObject").Unwrap();
        MethodInfo mi = bo.GetType().GetMethod("DoMagic", BindingFlags.Public | BindingFlags.Instance);
        var doMagicDelType = typeof(DoMagicDel<>).MakeGenericType(bo.GetType());            
        if (delDoMagic == null)
            delDoMagic = Delegate.CreateDelegate(doMagicDelType, null, mi);
        delDoMagic.DynamicInvoke(bo, s);
    }
}
#endregion


我希望我不太晚...

I hope I'm not too late ...

这篇关于通过反射创建打开的实例委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆