C#中的反射(Reflection)使用经典案例


C#中的反射(Reflection)是.NET框架提供的一种强大的运行时元编程机制,它允许程序在运行时获取类型信息、创建对象实例、调用方法、访问字段和属性等,而这些操作在编译时可能是未知的。以下是几个使用反射的典型场景:

1. 动态加载和调用类的方法

假设有一个库包含多个实现了同一接口的类,用户可以通过配置文件指定要使用的具体类名和方法名。通过反射,可以在运行时根据配置加载相应的类型,并调用指定的方法。

// 假设有个接口和其实现类
public interface ICalculator
{
    int Calculate(int a, int b);
}

public class Adder : ICalculator
{
    public int Calculate(int a, int b) => a + b;
}

// 配置中读取类名
string className = "Adder";
Type calculatorType = Type.GetType(className);

// 创建实例并调用方法
ICalculator calculator = (ICalculator)Activator.CreateInstance(calculatorType);
int result = calculator.Calculate(3, 5);

2. 记录用户修改行为

如您提到的应用场景,系统需要记录用户修改了哪个实体类的哪些字段。通过反射,可以遍历实体类的所有属性,在用户修改后记录下变化的属性名和新旧值。

public class User
{
    public string Name { get; set; }
    public int Age { get; set; }
    // 其他属性...
}

// 用户更新了一个User实例
var user = new User { Name = "OldName", Age = 30 };

foreach (var property in properties)
{
    // 获取旧值(假设这是更改前的值)
    var oldValue = property.GetValue(user);

    // 模拟用户更改属性值
    if (property.Name == "Name")
    {
        user.Name = "NewName";
    }

    // 再次获取新值
    var currentValue = property.GetValue(user);

    // 如果旧值与当前值不相等,则记录变更
    if (!object.Equals(oldValue, currentValue) && property.CanRead && property.CanWrite)
    {
        LogChange(property.Name, oldValue, currentValue);
    }
}

3. 调用私有构造函数

反射还可以用来调用非公开的构造函数,比如破坏单例模式时可能会用到:

public sealed class Singleton
{
    private static readonly Singleton instance = new Singleton();

    private Singleton() { }

    public static Singleton Instance => instance;

    // 通过反射破坏单例模式
    public static Singleton CreateAnotherInstance()
    {
        ConstructorInfo ctor = typeof(Singleton).GetConstructor(
            BindingFlags.Instance | BindingFlags.NonPublic,
            null, Type.EmptyTypes, null);
        return (Singleton)ctor.Invoke(null);
    }
}

4. 泛型类型的动态创建和使用

反射结合泛型,可以实现在不知道具体类型参数的情况下动态创建泛型类型实例:

public class GenericClass<T>
{
    public T Value { get; set; }
}

Type genericType = typeof(GenericClass<>).MakeGenericType(typeof(string));
dynamic instance = Activator.CreateInstance(genericType);
instance.Value = "Hello, World!";

C#反射除了上述提到的几个典型应用场景外,还有以下一些常见的用途:

5. 动态类型转换与检查

  • 判断一个对象是否实现了某个接口或继承自某个类。
  • 在不知道具体类型的情况下,将对象动态转换为指定类型。
object obj = new MyDerivedClass();
Type type = obj.GetType();

// 检查类型是否实现了某个接口
bool isDisposable = typeof(IDisposable).IsAssignableFrom(type);

// 动态转换
if (type == typeof(MyDerivedClass))
{
    MyDerivedClass derivedObj = (MyDerivedClass)obj;
    // 使用转换后的对象...
}

6. 获取和设置私有、受保护成员

  • 反射可以访问私有字段、属性和方法,这在测试框架中特别有用,可以模拟对私有成员的调用或者验证其值。
public class MyClass
{
    private int myPrivateField;
    
    public void SetPrivateValue(int value)
    {
        this.myPrivateField = value;
    }
}

var instance = new MyClass();
FieldInfo field = typeof(MyClass).GetField("myPrivateField", BindingFlags.NonPublic | BindingFlags.Instance);
field.SetValue(instance, 42);  // 设置私有字段值

int fieldValue = (int)field.GetValue(instance);  // 获取私有字段值

7. 枚举程序集、模块、类型等信息

  • 在大型应用程序中,可能需要枚举整个程序集中所有类型的元数据信息,比如获取所有的类名、特性(Attributes)等。
Assembly assembly = Assembly.GetExecutingAssembly();
foreach (Type type in assembly.GetTypes())
{
    Console.WriteLine($"Type: {type.FullName}");

    foreach (Attribute attribute in Attribute.GetCustomAttributes(type))
    {
        Console.WriteLine($"  Attribute: {attribute.GetType().Name}");
    }
}

8. 处理泛型类型参数

  • 反射可以帮助获取泛型类型的具体参数类型,并据此创建特定类型的实例。

9. 动态生成代码或动态编译

  • .NET Framework 和 .NET Core 提供了 System.Reflection.Emit 命名空间,允许开发者在运行时动态生成类型和方法。这对于实现AOP(面向切面编程)、动态代理或其他高级编程技术非常有用。

10. 配置驱动的应用程序扩展

  • 反射常用于构建插件式架构,根据配置文件加载不同插件(DLL)并在运行时动态加载并执行插件中的代码。

总之,C#反射是一个强大的工具,它允许程序在运行时获得关于类型和程序集的信息,并基于这些信息进行操作,极大地增强了应用程序的灵活性和适应性。但需要注意的是,过度使用反射可能会降低性能,应谨慎权衡其带来的便利性和潜在的成本。

注意事项:

  • 反射通常会带来性能开销,因为它涉及到运行时类型查找和动态方法调用。
  • 安全性方面,过度依赖反射可能导致代码容易受到攻击,例如绕过私有成员保护机制。
  • 静态编译优化对反射调用可能不适用,因此在对性能敏感的场合应谨慎使用反射。

python推荐学习汇总连接:
50个开发必备的Python经典脚本(1-10)

50个开发必备的Python经典脚本(11-20)

50个开发必备的Python经典脚本(21-30)

50个开发必备的Python经典脚本(31-40)

50个开发必备的Python经典脚本(41-50)
————————————————

​最后我们放松一下眼睛
在这里插入图片描述

  • 27
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

极致人生-010

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值