简体   繁体   English

如何使用 InvokeMember 通过反射调用泛型方法

[英]How to call generic method by reflection using InvokeMember

I need to call a method via reflection.我需要通过反射调用一个方法。 But the thing is that I don't want to find a method, I want to evaluate it at run-time using arguments.但问题是我不想找到一种方法,我想在运行时使用 arguments 对其进行评估。 This is what I try to do:这就是我尝试做的事情:

class Program
{
    static void Main(string[] args)
    {
        var type = typeof(Test<int>);
        type.InvokeMember("Write", BindingFlags.Instance | BindingFlags.Public | BindingFlags.InvokeMethod,
            null, new Test<int>(), new object[] {1, "1"});
        // The next call fails
        type.InvokeMember("Write", BindingFlags.Instance | BindingFlags.Public | BindingFlags.InvokeMethod,
            null, new Test<int>(), new object[] { 2, 2 });
    }
}

public class Test<T>
{
    public void Write(T arg1, string arg2)
    {
        Console.Write(arg1);
        Console.Write(arg2);
        Console.WriteLine(" write1");
    }

    public void Write<T2>(T arg1, T2 arg2)
    {
        Console.Write(arg1);
        Console.Write(arg2);
        Console.WriteLine(" write2");
    }
}

The first call works fine but the second one generates an exception saying that Write() was not found.第一个调用工作正常,但第二个调用生成一个异常,指出未找到 Write()。 Can I call it using InvokeMember anyhow?我可以使用 InvokeMember 调用它吗? I don't want trying to look for all methods and then call something like MakeGenericMethod()我不想尝试查找所有方法,然后调用 MakeGenericMethod()

Your second invoke try to call Write method with two int input but doesn't find any method with this signature in Test class.您的第二次调用尝试使用两个 int 输入调用Write方法,但在Test class 中找不到具有此签名的任何方法。

You can move T2 to class definition like this: public class Test<T, T2> and set T2 in type declaration like this: var type = typeof(Test<int,int>)您可以将T2移动到 class 定义,如下所示: public class Test<T, T2>并在类型声明中设置T2 ,如下所示: var type = typeof(Test<int,int>)

Final Code:最终代码:

class Program
    {
        static void Main(string[] args)
        {
            var type = typeof(Test<int, long>);
            var instance = Activator.CreateInstance(type);
            type.InvokeMember("Write", BindingFlags.Instance | BindingFlags.Public | BindingFlags.InvokeMethod,
                null, instance, new object[] { 1, "1" });

            type.InvokeMember("Write", BindingFlags.Instance | BindingFlags.Public | BindingFlags.InvokeMethod,
                null, instance, new object[] { 2, 22 });
        }
    }

    public class Test<T, T2>
    {
        public void Write(T arg1, string arg2)
        {
            Console.Write(arg1);
            Console.Write(arg2);
            Console.WriteLine(" write1");
        }

        public void Write(T arg1, T2 arg2)
        {
            Console.Write(arg1);
            Console.Write(arg2);
            Console.WriteLine(" write2");
        }
    }

Seems to be it is not possible.似乎是不可能的。 Remarks section in the documentation here explicitly says that 此处文档中的备注部分明确表示

You cannot use InvokeMember to invoke a generic method.您不能使用InvokeMember调用泛型方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM