C#反射的概念与实战

一、什么是反射?

  1. 一句话定义

    反射是一种"在运行时(Run-Time)而不是编译时(Compile-Time)去探查、创建、调用、修改程序集(Assembly)、类型(Type)、成员(Member)"的 API。

  2. 类比

    把程序当成一辆行驶中的高铁:

    • 正常情况下,我们只能在车站(编译期)知道每节车厢(类)里有哪些座位(字段/属性/方法)。

    • 有了反射,就相当于在高铁飞驰时还能临时打开任意一节车厢、查看座位、甚至把乘客换到别的车厢。

  3. 反射能做什么

    • 动态加载 dll(插件式架构)

    • 读取/设置私有成员(序列化、ORM、Mock)

    • 根据配置文件字符串创建对象(工厂模式)

    • 读取自定义 Attribute(AOP、验证框架)

    • 运行时生成代理(动态代理、远程调用)

二、核心 API 速查表

| 类/命名空间 | | 作用 | |----| | | 常用成员 | |------| | | 备注 | |----| |
|-----------------------------------|-------------------------|-------------------------------------------------------------------|---------------------------|
| System.Type | 描述"一个类型"的所有元数据 | GetMethods(), GetProperties(), GetFields(), GetCustomAttributes() | typeof(T) 或 obj.GetType() |
| System.Reflection.Assembly | |---------| | 描述一个程序集 | | Load(), LoadFrom(), GetTypes() | dll/exe 文件 |
| System.Reflection.PropertyInfo | |------| | 描述属性 | | GetValue(), SetValue() | 支持索引器 |
| System.Reflection.MethodInfo | |------| | 描述方法 | | Invoke() | 静态方法传 null 为实例 |
| System.Reflection.ConstructorInfo | 描述构造函数 | Invoke() | 创建实例 |
| System.Activator | 快捷创建对象 | CreateInstance(Type) | 内部还是调用 ConstructorInfo |

三、最小可运行案例

1:运行时探查对象

cs 复制代码
using System;
using System.Reflection;

class Person
{
    public string Name { get; set; }
    private int age;
    public Person(int age) { this.age = age; }
    private void SayHi() => Console.WriteLine($"Hi, {Name}, age {age}");
}

class Program
{
    static void Main()
    {
        Person p = new Person(18) { Name = "Tom" };
        Type t = p.GetType();                       // 1. 拿到 Type
        Console.WriteLine($"类名:{t.FullName}");

        // 2. 遍历属性
        foreach (var prop in t.GetProperties())
            Console.WriteLine($"属性:{prop.Name}={prop.GetValue(p)}");

        // 3. 读取私有字段
        FieldInfo ageField = t.GetField("age", BindingFlags.NonPublic | BindingFlags.Instance);
        Console.WriteLine($"私有字段 age={ageField.GetValue(p)}");

        // 4. 调用私有方法
        MethodInfo hi = t.GetMethod("SayHi", BindingFlags.NonPublic | BindingFlags.Instance);
        hi.Invoke(p, null);
    }
}

运行结果

cs 复制代码
类名:Person
属性:Name=Tom
私有字段 age=18
Hi, Tom, age 18

四、最小可运行案例

2:动态加载外部 DLL(插件)

场景:主程序在不重新编译的情况下,加载一个计算插件。

  1. 新建类库项目 CalculatorPlugin,编译生成 CalculatorPlugin.dll

    cs 复制代码
    public class Adder
    {
        public double Compute(double a, double b) => a + b;
        public string Name => "加法器";
    }
  2. 主程序(控制台 exe)

    cs 复制代码
    using System;
    using System.Reflection;
    
    class Program
    {
        static void Main()
        {
            // 1. 运行时把 dll 读进来
            Assembly asm = Assembly.LoadFrom(@"plugins\CalculatorPlugin.dll");
    
            // 2. 找到类型 Adder
            Type adderType = asm.GetType("CalculatorPlugin.Adder");
    
            // 3. 创建实例
            object adder = Activator.CreateInstance(adderType);
    
            // 4. 调用方法
            MethodInfo compute = adderType.GetMethod("Compute");
            double result = (double)compute.Invoke(adder, new object[] { 3, 4 });
    
            // 5. 读取属性
            string name = (string)adderType.GetProperty("Name").GetValue(adder);
    
            Console.WriteLine($"{name} 结果:{result}");
        }
    }

    把 dll 放到 exe 同级的 plugins 目录即可运行。

五、最小可运行案例

3:利用 Attribute + 反射做简易验证框架

cs 复制代码
using System;
using System.Reflection;

[AttributeUsage(AttributeTargets.Property)]
class RangeAttribute : Attribute
{
    public int Min { get; }
    public int Max { get; }
    public RangeAttribute(int min, int max) { Min = min; Max = max; }
}

class Student
{
    [Range(0, 150)]
    public int Score { get; set; }
}

static class Validator
{
    public static bool Validate(object obj)
    {
        foreach (var prop in obj.GetType().GetProperties())
        {
            var range = prop.GetCustomAttribute<RangeAttribute>();
            if (range != null)
            {
                int value = (int)prop.GetValue(obj);
                if (value < range.Min || value > range.Max)
                {
                    Console.WriteLine($"{prop.Name}={value} 不在范围 {range.Min}-{range.Max}");
                    return false;
                }
            }
        }
        return true;
    }
}

class Program
{
    static void Main()
    {
        var stu = new Student { Score = 200 };
        bool ok = Validator.Validate(stu);   // 输出提示
    }
}

六、性能注意点

1.反射调用比直接调用慢 2~3 个数量级,频繁调用需缓存:

cs 复制代码
// 只获取一次 MethodInfo 并缓存
private static readonly MethodInfo _method = typeof(Foo).GetMethod("Bar");
  1. .NET 7+ 可用 source generatorsIncremental Generators 在编译期生成代码,消除运行时反射。

  2. 使用 Delegate.CreateDelegate 把 MethodInfo 转成强类型委托,可接近原生性能。

七、常见错误及排查清单

|-------------------------------------------------|-------------------------------------|------------------------|
| 错误信息 | | 原因 | |----| | | 解决 | |----| |
| System.NullReferenceException | GetMethod/GetField 返回 null | 忘记 BindingFlags |
| System.Reflection.TargetException | |---------------| | 实例方法用 null 调用 | | |--------| | 传入正确实例 | |
| System.Reflection.TargetParameterCountException | 参数个数不匹配 | 检查 Invoke 的 object[] |
| System.IO.FileNotFoundException | LoadFrom 路径不对 | 使用绝对路径或 AppDomain 相对路径 |

"反射就是 .NET 给我们的'运行时显微镜 + 手术刀':先用 Type 看,再用 Invoke/SetValue 切,就能把原本编译期写死的逻辑变成可配置、可扩展的活代码。"

相关推荐
StudyWinter16 分钟前
【C++】仿函数和回调函数
开发语言·c++·回调函数·仿函数
C4程序员39 分钟前
北京JAVA基础面试30天打卡14
java·开发语言·面试
黑客影儿1 小时前
Go特有的安全漏洞及渗透测试利用方法(通俗易懂)
开发语言·后端·安全·web安全·网络安全·golang·系统安全
你好,我叫C小白2 小时前
C语言 常量,数据类型
c语言·开发语言·数据类型·常量
小红帽2.02 小时前
从ioutil到os:Golang在线客服聊天系统文件读取的迁移实践
服务器·开发语言·golang
Zafir20243 小时前
Qt实现TabWidget通过addTab函数添加的页,页内控件自适应窗口大小
开发语言·c++·qt·ui
阿巴~阿巴~3 小时前
深入解析C++非类型模板参数
开发语言·c++
谷宇.3 小时前
【Unity3D实例-功能-拔枪】角色拔枪(三)IK的使用-紧握武器
游戏·unity·c#·unity3d·游戏开发·游戏编程·steam
用户8356290780515 小时前
C# 从 PDF 提取图片教程
后端·c#
小指纹5 小时前
河南萌新联赛2025第(六)场:郑州大学
java·开发语言·数据结构·c++·算法