C#反射机制

公司项目用到了反射,本人之前从未使用过,查阅资料,自己写写,竟然能跑通,遂,分享。至于原理,百度去。

新建一个类库,里面写个测试类,里面有各种方法,编译过后得到TestReflection.dll


            System.Reflection.Assembly ass = Assembly.LoadFrom("TestReflection.dll"); //加载DLL

            Type type = ass.GetType("TestReflection.BLLService");

            var bll = Activator.CreateInstance(type);//创建实例

            System.Reflection.MethodInfo me = type.GetMethod("StudentInfo");

            me.Invoke(bll,new object[] {"李白",129,DateTime.Now });//这边数组的值需要与方法中的参数位置对应

//这个就是测试类

namespace TestReflection
{
   public class BLLService
    {
        public void GetClassName() {
            Console.WriteLine(this.GetType().Name);
        }
        public void StudentInfo(string name,int age,DateTime datetime) {
            Console.WriteLine(string.Format("学生{0}:\t{1}岁\t生日{2}",name,age,datetime));
        }
    }

}

//结果




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值