c# 反射基础

反射:可以动态的加载dll,然后解析meadata,读取元数据,类,方法,属性,字段,特性等,可以动态的修改代码,反射需要装箱拆箱,运行效率较慢,可以避开编译器检查,反射还可以破坏私有变量,破坏单例

反射可以使我们的程序方便扩展,做成可配置程序

反射一般分为以下几步:

1.加载dll 有三种方式

Assembly assembly1 = Assembly.Load("Test.DB.MySql");//不需要后缀

Assembly assembly1 = Assembly.LoadFile(@"G:\c#类库\c#\Reflect\bin\Debug\Test.DB.MySql.dll");//整个路径

Assembly assembly1 = Assembly.LoadFrom("Test.DB.MySql.dll");//需要加后缀

2.获取类型信息

Type typeDBHelper = assembly1.GetType("Test.DB.MySql.MySqlHelper");//获取类型信息

3.创建对象

object odbhelper = Activator.CreateInstance(typeDBHelper);//创建对象

4.转换,并调用

IDBHelper dBHelper = odbhelper as IDBHelper;//转换类型

dBHelper.Query();//方法调用

 

 

反射调用构造函数

Assembly assembly = Assembly.Load("Test.DB.SqlSever");//加载dll

Type testType = assembly.GetType("Test.DB.SqlSever.ReflectionTest");//获取类型信息

object otest1 = Activator.CreateInstance(testType);//默认无参构造函数

object otest2 = Activator.CreateInstance(testType, new object[] { });//无参构造函数

object otest3 = Activator.CreateInstance(testType, new object[] { 895 });//有参构造函数 类型是int

object otest4 = Activator.CreateInstance(testType, new object[] { "你好" });//有参构造函数,参数是string类型

 

Type genericType = assembly.GetType("Test.DB.SqlSever.GenericClass`3");//泛型 多一步需要指 定类型,代表有3个泛型参数的类

Type genericNewType = genericType.MakeGenericType(typeof(int), typeof(string), typeof(Program));//指定类上的三个泛型参数类型

object oGeneric = Activator.CreateInstance(genericNewType);//创建对象

MethodInfo mi = genericNewType.GetMethod("Show");//获取方法

Program p = new Program();

mi.Invoke(oGeneric, new object[] { 45, "你好", p });//调用方法

//MethodInfo dmi = mi.MakeGenericMethod(typeof(int), typeof(string), typeof(int));//指定泛型方法 的泛型类型

泛型类中的泛型方法,我们需要先指定泛型类上的泛型类型,泛型方法不需要指定类型,因为泛型类中已经指定过了

 

反射方法:

Assembly assembly = Assembly.Load("Test.DB.SqlSever");

Type testType = assembly.GetType("Test.DB.SqlSever.ReflectionTest");

object oTest = Activator.CreateInstance(testType);

{

MethodInfo method = testType.GetMethod("Show1");//无参方法

method.Invoke(oTest, null);

}

{

MethodInfo method = testType.GetMethod("Show2");//有参 类型是int

method.Invoke(oTest, new object[] { 12 });

}

{

MethodInfo method = testType.GetMethod("Show5");

method.Invoke(null, new object[] { "静态" });//静态可以写也可以不写

method.Invoke(oTest, new object[] { "静态" });

}

{

MethodInfo method = testType.GetMethod("Show3", new Type[] { typeof(int) });//重载

method.Invoke(oTest, new object[] { 45 });

}

{

MethodInfo method = testType.GetMethod("Show3", new Type[] { typeof(string), typeof(int) });//重载

method.Invoke(oTest, new object[] { "hjjj", 4566 });

}

{

MethodInfo method = testType.GetMethod("Show4", BindingFlags.Instance | BindingFlags.NonPublic);//私有方法

method.Invoke(oTest, new object[] { "嘿嘿" });

}

{

Type genericType = assembly.GetType("Test.DB.SqlSever.GenericMethod");//泛型方法

object oGeneric = Activator.CreateInstance(genericType);//创建对象

MethodInfo method = genericType.GetMethod("Show");//获取方法

MethodInfo methodNew = method.MakeGenericMethod(typeof(int), typeof(string), typeof(float)); Console.WriteLine(typeof(float));//指定泛型类型

methodNew.Invoke(oGeneric, new object[] { 455, "你好", 45.6f });//调用

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值