C# 如何使用refelection调用函数

C# 如何使用refelection调用函数,c#,C#,嗨 如何使用反射调用此函数 例如: void testFunction(int a , int b , out b) void testFunction(int a, int b, ref b) 这就是你想要的吗 using System; using System.Reflection; class Test { public void testFunction1(int a, int b, ref int c) { c += a + b; }

嗨 如何使用反射调用此函数

例如:

void testFunction(int a , int b , out b)
void testFunction(int a, int b, ref b)

这就是你想要的吗

using System;
using System.Reflection;

class Test
{
    public void testFunction1(int a, int b, ref int c)
    {
        c += a + b;
    }

    public void testFunction2(int a, int b, out int c)
    {
        c = a + b;
    }

    static void Main()
    {
        MethodInfo method = typeof(Test).GetMethod("testFunction1");
        object[] args = new object[] { 1, 2, 3 };
        Test instance = new Test();
        method.Invoke(instance, args);
        // The ref parameter will be updated in the array, so this prints 6
        Console.WriteLine(args[2]);

        method = typeof(Test).GetMethod("testFunction2");
        // The third argument here has to be of the right type,
        // but the method itself will ignore it
        args = new object[] { 1, 2, 999 };
        method.Invoke(instance, args);
        // The ref parameter will be updated in the array, so this prints 3
        Console.WriteLine(args[2]);
    }
}
请注意,如果要在以后获取更新的ref/out参数值,必须保留对用于指定参数的数组的引用


如果需要非公共方法,则需要在调用
GetMethod

时指定
BindingFlags

using System;
using System.Reflection;

class Test
{
    public void testFunction1(int a, int b, ref int c)
    {
        c += a + b;
    }

    public void testFunction2(int a, int b, out int c)
    {
        c = a + b;
    }

    static void Main()
    {
        MethodInfo method = typeof(Test).GetMethod("testFunction1");
        object[] args = new object[] { 1, 2, 3 };
        Test instance = new Test();
        method.Invoke(instance, args);
        // The ref parameter will be updated in the array, so this prints 6
        Console.WriteLine(args[2]);

        method = typeof(Test).GetMethod("testFunction2");
        // The third argument here has to be of the right type,
        // but the method itself will ignore it
        args = new object[] { 1, 2, 999 };
        method.Invoke(instance, args);
        // The ref parameter will be updated in the array, so this prints 3
        Console.WriteLine(args[2]);
    }
}
请注意,如果要在以后获取更新的ref/out参数值,必须保留对用于指定参数的数组的引用


如果需要非公共方法,则需要在对
GetMethod
的调用中指定
BindingFlags
,首先,该函数需要是类的成员:

class MyClass
{
    public: void testFunction(int a , int b , out b);
}
然后,您需要该类的一个实例:

MyClass myObj = new MyClass();
然后,使用反射获取函数:

MethodInfo[] myMethods = typeof(myObj).GetMethods(BindingFlags.Public);
遍历这些方法以找到所需的方法:

MethodInfo myTarget = NULL;
foreach(MethodInfo mi in myMethods)
{
    if (mi.Name == "testFunction")
    {
        myTarget = mi;
        break;
    }
}
最后,调用它:

int[] myParams = {1, 2, 3};
myTarget.Invoke(myObj, (object[])myParams);

首先,该函数必须是类的成员:

class MyClass
{
    public: void testFunction(int a , int b , out b);
}
然后,您需要该类的一个实例:

MyClass myObj = new MyClass();
然后,使用反射获取函数:

MethodInfo[] myMethods = typeof(myObj).GetMethods(BindingFlags.Public);
遍历这些方法以找到所需的方法:

MethodInfo myTarget = NULL;
foreach(MethodInfo mi in myMethods)
{
    if (mi.Name == "testFunction")
    {
        myTarget = mi;
        break;
    }
}
最后,调用它:

int[] myParams = {1, 2, 3};
myTarget.Invoke(myObj, (object[])myParams);

假设我想使用minfo.invoke(testerclass,新对象[]{aNewParam})进行调用;函数的第二个参数为outparameter@Ramesh:将提供在我到达火车站后10分钟内使用out参数的示例;类测试器{void testerfunction(示例a,out b)};BindingFlags eFlags=BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;MethodInfo mInfo=typeof(Tester).GetMethod(“testerfunction”,eFlags);Invoke(Tester类实例,新对象[]{aNewParam});假设我想使用minfo.invoke(testerclass,新对象[]{aNewParam})进行调用;函数的第二个参数为outparameter@Ramesh:将提供在我到达火车站后10分钟内使用out参数的示例;类测试器{void testerfunction(示例a,out b)};BindingFlags eFlags=BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;MethodInfo mInfo=typeof(Tester).GetMethod(“testerfunction”,eFlags);Invoke(Tester类实例,新对象[]{aNewParam});一点背景知识会有所帮助。你试过什么?你在哪里遇到麻烦的?你能给我们展示一些接近完整课堂的东西吗?一点背景知识会有所帮助。你试过什么?你在哪里遇到麻烦的?你能给我们看一些接近全班的东西吗?