C# 使用反射 理解GetMethods,GetParameters,MethodInfo.Invoke的使用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Reflection;

namespace ReflectionTest
{
    class MyClass
    {
        public int a;
        public int b;
        MyClass(int x, int y)
        {
            a = x;
            b = y;
        }
        public static int ShowData(int x, string m)
        {
            Console.WriteLine(" x = {0}, m = {1}", x, m);
            return 100;
        }
    }
    public class Progam
    {
        static void Main(string[] args)
        {
            Type ty = typeof(MyClass);
            MethodInfo[] info = ty.GetMethods();
            foreach(MethodInfo func in info)
            {
                if(func.Name.Equals("ShowData", StringComparison.Ordinal))
                {
                    object[] paramArray = {0,0};
                    ParameterInfo[] parmInfo = func.GetParameters();
                    foreach(ParameterInfo param in parmInfo)
                    {
                        Type paramType = param.ParameterType;

                        if(paramType.Name == "Int32")
                        {
                            paramArray[0] = Convert.ToInt32("10000000");
                        }
                        else if (paramType.Name == "String")
                        {
                            paramArray[1] = Convert.ToString("hello world ");
                        }
                    }
                    object result = func.Invoke(null,paramArray);
                    Console.WriteLine(" result = {0}", (int)result);
                }
            }
            Console.ReadKey();
        }
    }
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
反射Method类的使用可以通过以下步骤: 1. 获取Class对象:首先需要获取要反射的类的Class对象,可以通过类名调用Class.forName()方法或者使用类的class属性获取。 2. 获取Method对象:接下来需要获取要反射的方法的Method对象,可以通过Class对象的getMethod()、getDeclaredMethod()、getMethods()、getDeclaredMethods()等方法获取。 3. 设置方法可访问性:如果要反射的方法是私有的,需要设置方法的可访问性为true,否则会抛出IllegalAccessException异常。 4. 调用方法:最后,使用Method对象的invoke()方法调用方法,并传入方法所属的对象和方法参数,如果方法没有返回值,invoke()方法返回null,否则返回方法返回值。 以下是一个示例代码,演示了如何使用反射Method类来调用一个类的方法: ```java public class MyClass { private void myMethod(String str, int num) { System.out.println("My method: " + str + ", " + num); } } public class Main { public static void main(String[] args) throws Exception { MyClass obj = new MyClass(); Class<?> cls = obj.getClass(); Method method = cls.getDeclaredMethod("myMethod", String.class, int.class); method.setAccessible(true); method.invoke(obj, "Hello", 123); } } ``` 在上述示例代码中,我们首先创建了一个MyClass类,并定义了一个私有的myMethod()方法。然后我们通过Class类的getDeclaredMethod()方法获取了myMethod()方法的Method对象,并设置其可访问性为true。最后,我们使用Method对象的invoke()方法调用了myMethod()方法,并传入了一个字符串和一个整数作为参数。运行上述代码,输出结果为: ``` My method: Hello, 123 ``` 这说明我们成功地使用反射Method类调用了一个类的私有方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值