反射(Reflection) 是 .NET 提供的一种机制,允许你在运行时动态地分析、检查和操作程序集中的类型(如类、接口、方法、属性等)。
场景 | 示例 |
---|---|
自动测试工具 | 使用反射查找所有测试类和方法 |
序列化/反序列化 | 动态读取对象属性进行 JSON 映射 |
插件系统 | 动态加载 DLL 并调用其中的方法 |
依赖注入容器 | 根据类型自动解析构造函数参数 |
Type type = typeof(string);
Console.WriteLine("类型名称:" + type.FullName);
object obj = "Hello";
Type runtimeType = obj.GetType();
Type type = typeof(string);
object instance = Activator.CreateInstance(type);
注意:对于有参数的构造函数,需要传递参数数组。
MethodInfo method = type.GetMethod("MethodName", new[] { typeof(int) });
method.Invoke(instance, new object[] { 42 });
PropertyInfo prop = type.GetProperty("Name");
prop.SetValue(instance, "张三");
object value = prop.GetValue(instance);
反射虽然强大,但性能较低。以下是一些优化方式:
方法 | 说明 |
---|---|
Expression Trees |
构建委托缓存以提高重复调用性能 |
Delegate.CreateDelegate |
将 MethodInfo 转换为委托 |
缓存反射结果 |
避免重复调用 GetMethod / GetProperty |
特性(Attribute) 是一种元数据机制,用于向代码元素(如类、方法、属性)附加额外的信息。
这些信息可以在编译时或运行时通过反射读取。
特性 | 说明 |
---|---|
[Obsolete] |
标记某个方法已过时 |
[Serializable] |
表示该类可被序列化 |
[DllImport] |
用于调用非托管 DLL 函数 |
[Conditional] |
控制方法是否在特定条件下编译 |
[Obsolete("请使用 NewMethod 替代")]
public void OldMethod()
{
// ...
}
你可以定义自己的特性类,继承 Attribute
:
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]
public class AuthorAttribute : Attribute
{
public string Name { get; }
public AuthorAttribute(string name)
{
Name = name;
}
}
然后应用到类或方法上:
[Author("李四")]
public class MyService
{
[Author("王五")]
public void DoWork()
{
// ...
}
}
Type type = typeof(MyService);
// 获取类上的特性
foreach (var attr in type.GetCustomAttributes(false))
{
if (attr is AuthorAttribute author)
{
Console.WriteLine($"类作者:{author.Name}");
}
}
// 获取方法上的特性
MethodInfo method = type.GetMethod("DoWork");
foreach (var attr in method.GetCustomAttributes(true))
{
if (attr is AuthorAttribute author)
{
Console.WriteLine($"方法作者:{author.Name}");
}
}
public interface IPlugin
{
void Execute();
}
[AttributeUsage(AttributeTargets.Class)]
public class PluginAttribute : Attribute { }
[Plugin]
public class HelloPlugin : IPlugin
{
public void Execute()
{
Console.WriteLine("你好,插件!");
}
}
class Program
{
static void Main()
{
foreach (var type in Assembly.GetExecutingAssembly().GetTypes())
{
if (type.GetCustomAttributes(typeof(PluginAttribute), false).Length > 0 &&
typeof(IPlugin).IsAssignableFrom(type))
{
var plugin = (IPlugin)Activator.CreateInstance(type);
plugin.Execute();
}
}
}
}
今天你学会了:
反射与特性是 C# 中非常强大的两个机制,它们广泛应用于框架开发、自动化测试、ORM 工具、DI 容器等领域。
明天我们将进入一个新的主题 —— 动态类型与动态语言运行时(Dynamic Types & DLR),你将学会如何使用 dynamic
类型简化反射操作,并理解它背后的原理。