C#的反射机制是.NET框架提供的重要特性,它允许程序在运行阶段动态获取程序集、模块、类型等元数据信息,并且可以对这些类型成员进行调用和操作,不需要在编译期确定具体的类型。这种特性在很多框架开发、插件系统、序列化场景中有着广泛的应用。

反射的基础入口:Type类
在C#中,所有反射操作的起点都是System.Type类,每个加载到内存中的类型都会对应一个Type实例,通过这个实例可以获取该类型的所有元数据信息。获取Type对象的方式有多种,最常用的是以下三种:
- 通过对象的
GetType()方法获取,适用于已经有类型实例的场景 - 通过
typeof()运算符获取,适用于编译期已知类型的场景 - 通过
Assembly.GetType()方法获取,适用于从程序集动态加载类型的场景
获取类型的基础信息
获取到Type对象之后,就可以读取类型的基础属性,比如类型名称、命名空间、是否为抽象类、是否为接口等信息。下面是一个简单的示例:
using System;
namespace ReflectionDemo
{
// 定义一个测试类
public class User
{
public string Name { get; set; }
public int Age { get; set; }
public void SayHello()
{
Console.WriteLine($"Hello, my name is {Name}");
}
}
class Program
{
static void Main(string[] args)
{
// 方式1:通过typeof获取Type对象
Type userType = typeof(User);
// 输出类型基础信息
Console.WriteLine($"类型全名称:{userType.FullName}");
Console.WriteLine($"命名空间:{userType.Namespace}");
Console.WriteLine($"类型名称:{userType.Name}");
Console.WriteLine($"是否为公共类:{userType.IsPublic}");
Console.WriteLine($"是否为抽象类:{userType.IsAbstract}");
Console.WriteLine($"是否为接口:{userType.IsInterface}");
Console.WriteLine($"基类类型:{userType.BaseType?.Name}");
}
}
}
获取类型的成员信息
除了基础信息,反射还可以获取类型的成员,包括属性、方法、构造函数、字段等,这些成员信息都对应着System.Reflection命名空间下的不同类型,比如PropertyInfo对应属性,MethodInfo对应方法。
获取属性信息
通过Type的GetProperties()方法可以获取类型的所有公共属性,返回值是PropertyInfo[]数组,每个PropertyInfo对象包含属性的名称、类型、是否可读可写等信息。
using System;
using System.Reflection;
namespace ReflectionDemo
{
public class User
{
public string Name { get; set; }
public int Age { get; set; }
private string Id { get; set; }
}
class Program
{
static void Main(string[] args)
{
Type userType = typeof(User);
// 获取所有公共属性,不包含私有属性
PropertyInfo[] properties = userType.GetProperties();
Console.WriteLine("公共属性列表:");
foreach (PropertyInfo property in properties)
{
Console.WriteLine($"属性名:{property.Name},属性类型:{property.PropertyType.Name}");
Console.WriteLine($"是否可读:{property.CanRead},是否可写:{property.CanWrite}");
}
// 如果获取包含私有属性,需要指定绑定标志
PropertyInfo[] allProperties = userType.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
Console.WriteLine("n所有属性列表(包含私有):");
foreach (PropertyInfo property in allProperties)
{
Console.WriteLine($"属性名:{property.Name},访问修饰符:{(property.GetGetMethod(true)?.IsPrivate ?? false ? "私有" : "公共")}");
}
}
}
}
获取方法信息
通过GetMethods()方法可以获取类型的所有公共方法,返回MethodInfo[]数组,每个MethodInfo包含方法的名称、返回类型、参数列表等信息。
using System;
using System.Reflection;
namespace ReflectionDemo
{
public class User
{
public void SayHello()
{
Console.WriteLine("Hello");
}
public int Add(int a, int b)
{
return a + b;
}
private void PrivateMethod()
{
Console.WriteLine("私有方法");
}
}
class Program
{
static void Main(string[] args)
{
Type userType = typeof(User);
// 获取公共方法
MethodInfo[] methods = userType.GetMethods();
Console.WriteLine("公共方法列表:");
foreach (MethodInfo method in methods)
{
// 过滤掉从Object继承的方法
if (method.DeclaringType == typeof(User))
{
Console.WriteLine($"方法名:{method.Name},返回类型:{method.ReturnType.Name}");
ParameterInfo[] parameters = method.GetParameters();
Console.WriteLine($"参数数量:{parameters.Length}");
if (parameters.Length > 0)
{
foreach (ParameterInfo param in parameters)
{
Console.WriteLine($"参数名:{param.Name},参数类型:{param.ParameterType.Name}");
}
}
}
}
}
}
}
获取构造函数信息
构造函数也是类型的成员之一,通过GetConstructors()方法可以获取类型的公共构造函数,返回ConstructorInfo[]数组,可以用于后续动态创建类型的实例。
using System;
using System.Reflection;
namespace ReflectionDemo
{
public class User
{
public string Name { get; set; }
public int Age { get; set; }
// 无参构造函数
public User() { }
// 带参构造函数
public User(string name, int age)
{
Name = name;
Age = age;
}
}
class Program
{
static void Main(string[] args)
{
Type userType = typeof(User);
ConstructorInfo[] constructors = userType.GetConstructors();
Console.WriteLine("构造函数列表:");
foreach (ConstructorInfo constructor in constructors)
{
ParameterInfo[] parameters = constructor.GetParameters();
Console.WriteLine($"构造函数参数数量:{parameters.Length}");
if (parameters.Length > 0)
{
foreach (ParameterInfo param in parameters)
{
Console.WriteLine($"参数名:{param.Name},参数类型:{param.ParameterType.Name}");
}
}
}
}
}
}
反射获取类型信息的注意事项
虽然反射功能强大,但是使用时也有一些需要注意的点:
- 反射操作的性能比直接编译期类型调用要低,频繁使用反射的场景需要考虑缓存Type对象或者成员信息,减少重复获取的开销
- 获取私有成员时需要指定对应的绑定标志,默认情况下
GetProperties()、GetMethods()等方法只返回公共成员 - 反射可以绕过访问权限检查,操作私有成员时需要确认是否符合业务逻辑,避免破坏封装性
反射是C#中非常实用的特性,掌握运行时动态获取类型信息的方法,能够帮助开发者应对更多动态场景的开发需求,在框架设计、插件化开发等场景中发挥重要作用。
C#反射Reflection动态获取类型信息修改时间:2026-07-07 09:15:30