C# 反射(Reflection)

本文详细介绍了C#中的反射机制,包括反射的使用、动态实例化对象、调用不同参数方法、处理泛型以及单例模式的应用。强调了反射带来的灵活性和扩展性,但也指出其性能损失和代码复杂性的缺点。同时,列举了反射在IOC、MVC和ORM等框架中的常见用途。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C# 反射(Reflection)

首先说一下反射的优点:动态!!!

首先了解一下C#编译运行过程,大致如下所示:
在这里插入图片描述

首先被编译器编译成dll/exe,一般我们发布的都是这个东西,然后在运行的时候会被CLR/JIT编译成机器码。

为什么不直接通过编译器编译成机器码呢?答案就是:通过CLR/JIT可以根据不同的平台编译成不同的机器码,用以一次编译多平台运行。

而我们通过反射处理的就是matadata这一块,里面包含了我们写的类文件,方法等等。

微软提供的反射工具主要是 System.Reflection。

反射的使用

  1. 第一步:加载dll文件
	// 第一种:直接写dll文件名,不用加后缀
	Assembly assembly = Assembly.Load("DB.MySql1");
	// 第二种:使用完整路径加载。(可以是别的目录,不推荐使用,虽然不会错,但是如果没有对应的依赖项,使用会报错)
	Assembly assembly1 = Assembly.LoadFile(@"E:\学习\.net learn\Net_Learn\DB.MySql1\bin\Debug\DB.MySql1.dll");
	// 第三种:使用类库名带后缀,或者直接路径
    Assembly assembly2 = Assembly.LoadFrom("DB.MySql1.dll");

通过上述的加载需要单设的文件后,我们可以获取部分信息:

	// GetModules()获取模块信息
	foreach (var item in assembly.GetModules())
	{
	    Console.WriteLine("获取模块信息:" + item.FullyQualifiedName);
	}
	
	// GetTypes()获取类型信息
    foreach (var item in assembly.GetTypes())
    {
        Console.WriteLine("获取类型信息:" + item.FullName);
    }
  1. 获取类型信息
	// 这里的 DB.MySql1.MySqlHelper 是 类库名.类名
	Type type = assembly.GetType("DB.MySql1.MySqlHelper");
  1. 创建对象
	// 创建对象:Activator.CreateInstance(type),与 new MySqlHelper() 效果一样
	object oDBHelper = Activator.CreateInstance(type);
	// 一般到了这里,我们觉得就可以直接像 new MySqlHelper() 之后一样去调用方法了
	// oHelper 是object,不能调用,但实际方法是有的,编译器不认可
	// oHelper.Query();

4 类型转换

	// 所以这里需要做类型转换
	IDBHelper iDBHelper = (IDBHelper)oDBHelper;

5 方法调用

iDBHelper.Query();

上述操作如果按照直接实例化的方式:

	IDBHelper dBHelper = new MySqlHelper();
    dBHelper.Query();

相比之下反射的操作比直接实例化要复杂一些。

为了避免每次写一堆代码,我们也可以写一个公共方法:

	/// <summary>
    /// 使用工厂类封装反射方法
    /// <para>更改配置文件就可以执行不同的操作,无需编译文件</para>
    /// </summary>
    public class Factory
    {
        // 读取配置文件中配置的 类库名加类名
        private static string IDBHelperConfig = ConfigurationManager.AppSettings["IDBHelperConfig"];
        // 类库名
        private static string DllName = IDBHelperConfig.Split(',')[1];
        // 类名
        private static string TypeName = IDBHelperConfig.Split(',')[0];

        public static IDBHelper CreateHelper()
        {
            //1 加载dll
            Assembly assembly = Assembly.Load(DllName);
            //2 获取类型信息
            Type type = assembly.GetType(TypeName);
            //3 创建对象
            object oDBHelper = Activator.CreateInstance(type);
            //4 类型转换
            IDBHelper iDBHelper = (IDBHelper)oDBHelper;
            return iDBHelper;
        }
    }

然后在配置文件中配置好:

	<add key="IDBHelperConfig" value="DB.MySql1.MySqlHelper,DB.MySql1"/>

后续我们就可以直接通过调用类方法来反射:

	// 反射的前四步就直接通过调用这个方法来处理好
	IDBHelper iDBHelper2 = Factory.CreateHelper();
	// 最后就可以直接调用里面的方法
	iDBHelper2.Query();

上述方法还可以根据自己的需求扩展。

反射+单例 实例化对象

单例:单例模式是确保一个类只有一个实例,并提供一个全局访问方式的设计方法。
这里我们将对反射实现单例:

	Assembly assembly3 = Assembly.Load("DB.SqlServer1");
    Type type1 = assembly3.GetType("DB.SqlServer1.Singleton");
    // 通过反射实例化单例对象(Singleton为已经写好的单例类)
    Singleton singleton3 = (Singleton)Activator.CreateInstance(type1, true);

反射+构建不同参数的方法

对于同一个类下有多部重载方法时:

	Assembly assembly3 = Assembly.Load("DB.SqlServer1");
    Type type1 = assembly3.GetType("DB.SqlServer1.ReflectionTest");

    // 调用无参构造函数
    object oReflectionTest1 = Activator.CreateInstance(type1);

    // 调用带参数,且参数是 int 
    object oReflectionTest2 = Activator.CreateInstance(type1, new object[] { 123 });

    // 调用带参数,且参数是 string
    object oReflectionTest3 = Activator.CreateInstance(type1, new object[] { "123" });

反射+泛型

	Assembly assembly3 = Assembly.Load("DB.SqlServer1");

    // 泛型类中 GenericClass<T, W, X> 的参数示意占位符的形式展现的,所以这里也已占位符的形式写
    Type type1 = assembly3.GetType("DB.SqlServer1.GenericClass`3");

    // 需要执行泛型参数的类型,否则会报错
    Type newType = type1.MakeGenericType(new Type[] { typeof(int), typeof(string), typeof(DateTime) });

    object oGenericClass1 = Activator.CreateInstance(newType);

反射+Method

	Assembly assembly3 = Assembly.Load("DB.SqlServer1");
    Type type1 = assembly3.GetType("DB.SqlServer1.ReflectionTest");
    object OReflectionTest = Activator.CreateInstance(type1);

    {
        // 调用没有参数的方法
        MethodInfo methodInfo = type1.GetMethod("Show1");
        methodInfo.Invoke(OReflectionTest, null);
    }

    {
        // 调用一个int型参数的方法
        MethodInfo methodInfo = type1.GetMethod("Show2");
        methodInfo.Invoke(OReflectionTest, new object[] { 1231 });
    }

    {
        // 调用一个string型参数的 静态方法 方法
        Console.WriteLine("静态方法");
        MethodInfo methodInfo = type1.GetMethod("Show5");
        // 静态方法有两种:
        // 1.跟其他一样
        methodInfo.Invoke(OReflectionTest, new object[] { "1231" });
        // 2.不用传递对象,因为静态方法无需实例,所以不用传递对象
        methodInfo.Invoke(null, new object[] { "1231" });
    }

    {
        Console.WriteLine("重载方法");
        // 调用没有参数的 重载 方法,重载方法在获取方法时需要指定参数类型,没有就是空
        MethodInfo methodInfo = type1.GetMethod("Show3", new Type[] { });
        methodInfo.Invoke(OReflectionTest, new object[] { });
    }

    {
        // 调用一个 int 参数的 重载 方法,重载方法在获取方法时需要指定参数类型
        MethodInfo methodInfo = type1.GetMethod("Show3", new Type[] { typeof(int) });
        methodInfo.Invoke(OReflectionTest, new object[] { 123 });
    }

    {
        // 调用一个 string 参数的 重载 方法,重载方法在获取方法时需要指定参数类型
        MethodInfo methodInfo = type1.GetMethod("Show3", new Type[] { typeof(string) });
        methodInfo.Invoke(OReflectionTest, new object[] { "123" });
    }

    {
        // 调用一个 string,一个int 参数的 重载 方法,重载方法在获取方法时需要指定参数类型
        MethodInfo methodInfo = type1.GetMethod("Show3", new Type[] { typeof(string), typeof(int) });
        methodInfo.Invoke(OReflectionTest, new object[] { "123", 13 });
    }

    {
        // 调用一个 int,一个 string 参数的 重载 方法,重载方法在获取方法时需要指定参数类型
        MethodInfo methodInfo = type1.GetMethod("Show3", new Type[] { typeof(int), typeof(string) });
        methodInfo.Invoke(OReflectionTest, new object[] { 13, "123" });
    }

	// 以上都是知道方法名的情况下,下面是不知道的情况下
	foreach (var item in type1.GetMethods())
	{
		// 可以遍历出所有的方法
	    Console.WriteLine(item.Name);
	}

	// 调用一个私有方法,传递一个string参数
    MethodInfo methodInfo = type1.GetMethod("Show4", BindingFlags.Instance | BindingFlags.NonPublic);
    methodInfo.Invoke(OReflectionTest, new object[] { "123" });

	// 调用泛型类,泛型方法
    Console.WriteLine("泛型类,泛型方法");
    // 泛型类中 GenericDouble<T> 的参数示意占位符的形式展现的,所以这里也已占位符的形式写
    Type type2 = assembly3.GetType("DB.SqlServer1.GenericDouble`1");

    // 需要执行泛型参数的类型,否则会报错
    // [泛型类一个参数]
    Type newType = type2.MakeGenericType(new Type[] { typeof(int) });

    object oGenericClass1 = Activator.CreateInstance(newType);

    MethodInfo methodInfo = newType.GetMethod("Show");
    // [泛型方法2个参数]
    MethodInfo methodInfoNew = methodInfo.MakeGenericMethod(new Type[] { typeof(string), typeof(DateTime) });
    // [传递的是三个参数]
    methodInfoNew.Invoke(oGenericClass1, new object[] { 132, "23", DateTime.Now });

反射+Property/field

	People people = new People();
    people.Id = 123;
    people.Name = "aaa";
    people.Description = "Description";

    Console.WriteLine($"peoplez.id={people.Id}");

    Type type1 = typeof(People);
    object oPeople = Activator.CreateInstance(type1);

    // GetProperties() 只能操作属性,带get,set的
    foreach (var item in type1.GetProperties())
    {
        Console.WriteLine("-------------------GetProperties---------------");
        // 属性
        Console.WriteLine($"{item.Name}");
        // 属性值
        Console.WriteLine($"{item.GetValue(oPeople)}");

        // 设置值
        if (item.Name.Equals("Id"))
        {
            item.SetValue(oPeople,789);
        }
        if (item.Name.Equals("Name"))
        {
            item.SetValue(oPeople, "aaa更改");
        }

        Console.WriteLine($"{type1.Name},{item.Name},{item.GetValue(oPeople)}");
    }

    // GetFields()只能操作字段 ,详见people类的 Description 字段,没有写get,set
    foreach (var item in type1.GetFields())
    {
        Console.WriteLine("-------------------GetFields---------------");

        if (item.Name.Equals("Description"))
        {
            item.SetValue(oPeople, "Description更改");
        }
        Console.WriteLine($"{type1.Name}.{item.Name}={item.GetValue(oPeople)}");
    }

总结

反射的类型创建 IOC里面用到。
反射获取方法 MVC里面用到。
反射获取属性,字段 ORM 里面用到。

优缺点
优点:
1、反射提高了程序的灵活性和扩展性。
2、降低耦合性,提高自适应能力。
3、它允许程序创建和控制任何类的对象,无需提前硬编码目标类。

缺点:
1、性能问题:使用反射基本上是一种解释操作,用于字段和方法接入时要远慢于直接代码。因此反射机制主要应用在对灵活性和拓展性要求很高的系统框架上,普通程序不建议使用。
2、使用反射会模糊程序内部逻辑;程序员希望在源代码中看到程序的逻辑,反射却绕过了源代码的技术,因而会带来维护的问题,反射代码比相应的直接代码更复杂。

反射(Reflection)有下列用途:

1、它允许在运行时查看特性(attribute)信息。
2、它允许审查集合中的各种类型,以及实例化这些类型。
3、它允许延迟绑定的方法和属性(property)。
4、它允许在运行时创建新类型,然后使用这些类型执行一些任务。

查看元数据
我们已经在上面的章节中提到过,使用反射(Reflection)可以查看特性(attribute)信息。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值