反射(Reflection)是 C# 中一种动态分析程序集、类型及成员的机制,允许在运行时获取类型信息、创建对象、调用方法或访问字段,无需在编译时明确知道具体类型。
1、动态类型操作
2、程序集分析
3、元数据访问
1、System.Type 类
获取类型的途径:
// 通过对象获取
Type type1 = obj.GetType();
// 通过类型名获取
Type type2 = typeof(int);
Type type3 = Type.GetType("System.String");
// 通过程序集获取
Assembly assembly = Assembly.Load("MyLibrary");
Type type4 = assembly.GetType("MyLibrary.MyClass");
常用方法:
2、System.Reflection.Assembly 类
// 从文件加载
Assembly asm1 = Assembly.LoadFrom("MyLibrary.dll");
// 通过程序集名加载
Assembly asm2 = Assembly.Load("MyLibrary");
foreach (Type type in asm1.GetTypes())
{
Console.WriteLine(type.FullName);
}
3、Activator 类
Type type = typeof(MyClass);
object instance = Activator.CreateInstance(type);
// 带参数的构造函数
object instance2 = Activator.CreateInstance(type, "参数1", 100);
4、MethodInfo 与调用方法
Type type = typeof(MyClass);
MethodInfo method = type.GetMethod("MyMethod");
object instance = Activator.CreateInstance(type);
// 调用无参方法
method.Invoke(instance, null);
// 调用有参方法
method.Invoke(instance, new object[] { "参数", 42 });
1、动态插件系统
Assembly pluginAsm = Assembly.LoadFrom("Plugin.dll");
Type pluginType = pluginAsm.GetType("Plugin.MyPlugin");
IPlugin plugin = (IPlugin)Activator.CreateInstance(pluginType);
plugin.Execute();
2、依赖注入(DI)框架
public static T Resolve<T>()
{
Type type = typeof(T);
ConstructorInfo ctor = type.GetConstructors().First();
ParameterInfo[] paramsInfo = ctor.GetParameters();
object[] args = paramsInfo.Select(p => Resolve(p.ParameterType)).ToArray();
return (T)ctor.Invoke(args);
}
3、序列化与反序列化
public static string ToJson(object obj)
{
var sb = new StringBuilder("{");
foreach (PropertyInfo prop in obj.GetType().GetProperties())
{
object value = prop.GetValue(obj);
sb.Append($"\"{prop.Name}\":\"{value}\",");
}
sb.Remove(sb.Length - 1, 1).Append("}");
return sb.ToString();
}
4、ORM(对象关系映射)
public static T MapToEntity<T>(DataRow row) where T : new()
{
T entity = new T();
foreach (DataColumn column in row.Table.Columns)
{
PropertyInfo prop = typeof(T).GetProperty(column.ColumnName);
if (prop != null && row[column] != DBNull.Value)
prop.SetValue(entity, row[column]);
}
return entity;
}
1、反射的性能问题
2、优化策略
private static readonly MethodInfo _cachedMethod = typeof(MyClass).GetMethod("MyMethod");
// 将 MethodInfo 转换为委托
Action<object, object[]> methodDelegate = (Action<object, object[]>)
Delegate.CreateDelegate(typeof(Action<object, object[]>), _cachedMethod);
var param = Expression.Parameter(typeof(MyClass));
var call = Expression.Call(param, _cachedMethod);
var lambda = Expression.Lambda<Action<MyClass>>(call, param).Compile();
lambda(obj); // 高速调用
1、安全性限制
C# 反射是处理动态类型和元数据的强大工具,广泛应用于插件系统、序列化、ORM 等场景。尽管其灵活性极高,但需谨慎使用以避免性能问题和代码维护困难。优化策略(如缓存、表达式树)和合理设计(如接口隔离)是高效使用反射的关键。