[No000085]C#反射Demo,通过类名(String)创建类实例,通过方法名(String)调用方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
class test
{
    public void Method()//1
    {
        Console.WriteLine("1:__" + "Method调用成功!");
    }
    public void Method(string str)//2
    {
        Console.WriteLine("2:__" + str);
    }

    public string Method(string str1, string str2)//3
    {
        string className1 = this.GetType().FullName;//非静态方法中获取类名
        string className2 = MethodBase.GetCurrentMethod().ReflectedType.FullName;//静态方法中的获取类名
        Console.WriteLine("3:__;类名"+className1+"_"+ className2);
        return "3:__" + str1 + str2;
    }
}
class Program
{
    public static void run(string testcase)
    {
        string strClass = "test";  //命名空间+类名
        string strMethod = "Method";//方法名

        Type type;
        object obj;

        type = Type.GetType(strClass);//通过string类型的strClass获得同名类“type”
        obj = System.Activator.CreateInstance(type);//创建type类的实例 "obj"

        MethodInfo method = type.GetMethod(strMethod, new Type[] { });//取的方法描述//通过string类型的strMethod获得同名的方法“method”//1
        method.Invoke(obj, null);//type类实例obj,调用方法"method"//1

        method = type.GetMethod(strMethod, new Type[] { typeof(String) });//取的方法描述//2
        object[] objs = new object[] { testcase };
        method.Invoke(obj, objs);//t类实例obj,调用方法"method(testcase)"//2

        method = type.GetMethod(strMethod, new Type[] { typeof(String), typeof(String) });//取的方法描述//2
        var result = (string)method.Invoke(obj, new object[] { "a", "b" });//3
        Console.WriteLine(result);//3

        //string className = this.GetType().FullName;
        string className = MethodBase.GetCurrentMethod().ReflectedType.FullName;//静态方法中的获取类名
        Console.WriteLine(className);
        Console.ReadKey();
    }
    static void Main(string[] args)
    {
        string testcase = "测试呀";//自己定义的类
        run(testcase);
    }
}

 

转载于:https://www.cnblogs.com/Chary/p/No000085.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值