用反射传递代表

时间:2013-07-12 14:01:38

标签: c# reflection delegates

我有一个类,它有一个获取委托参数的方法,并且可以用反射来调用,例如:

namespace NSa
{
    public delegate void dlg(string p1, string p2);

    public class dyn
    {
        void method(dlg d)
        {
            // call d...
        }
    }
}

在另一个课程中,我需要用反射调用dyn.method

namespace NSa
{
    public delegate void dlg(string p1, string p2);

    public void fun(string a, string b) { Console.Write(a); }

    public class other
    {
        void caller_method()
        {
            dlg x = fun;

            //... 

            var assembly = System.Reflection.Assembly.LoadFile("xx.dll");
            System.Reflection.BindingFlags flags = (System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.DeclaredOnly);
            System.Reflection.Module[] dynModules = assembly.GetModules();
            Type dynType = dynModules[0].GetType("NSa.dyn");
            object dynObj = Activator.CreateInstance(dynType);               
            System.Reflection.MethodInfo dynMethod = dynType.GetMethods(flags).Where(m => m.Name == "method").First();
            dynMethod.Invoke(dynObj, new object[] {x});             
        }
    }
}

我得到例外:

Object of type 'NSa.dlg' cannot be converted to type 'NSa.dlg'.

我错过了什么?

1 个答案:

答案 0 :(得分:1)

看起来你在两个不同的程序集中声明了两次相同的类型。就CLR而言,这将是两种完全不同的类型,它们之间没有转换。

您应该使用与dyn在同一程序集中声明的委托,以便将其传递给dyn.method。如果您在编译时不了解该程序集,则需要使用Delegate.CreateDelegate创建该方法的实例。

我还建议通过对不同的程序集使用不同的名称空间声明,更容易分辨哪个类型来自哪个程序集。