发布时间:2026-10-03 03:57:52 浏览次数:4
C# PropertyInfo类是System.Reflection命名空间下的一个类,用于描述类的属性成员。通过PropertyInfo类,可以获取类的属性的信息、值和元数据。
可以使用Type类的GetProperty()方法来获取类的属性信息,该方法接受一个字符串类型的参数,即要获取的属性名称。例如,下面是一个获取Student类中Name属性信息的例子:
class Program{ static void Main() { PropertyInfo propertyInfo = typeof(Student).GetProperty("Name"); Console.WriteLine(propertyInfo.Name); Console.WriteLine(propertyInfo.PropertyType); Console.WriteLine(propertyInfo.CanRead); Console.WriteLine(propertyInfo.CanWrite); }}class Student{ public string Name { get; set; } public int Age { get; set; } public string Address { get; set; }}输出结果:
NameSystem.StringTrueTrue通过PropertyInfo类,可以获取属性的值。使用GetValue()方法获取属性值,它接受一个对象参数,即要获取属性值的对象。例如,下面是一个获取Student类中Name属性值的例子:
class Program{ static void Main() { Student student = new Student { Name = "小明", Age = 18, Address = "北京市海淀区" }; PropertyInfo propertyInfo = typeof(Student).GetProperty("Name"); string name = (string)propertyInfo.GetValue(student); Console.WriteLine("Name is {0}", name); }}class Student{ public string Name { get; set; } public int Age { get; set; } public string Address { get; set; }}输出结果:
Name is 小明使用SetValue()方法进行设置属性值。它接受两个参数,第一个参数是要设置属性值的对象,第二个参数是要设置的属性值。例如,下面是一个设置Student类中Name属性值的例子:
class Program{ static void Main() { Student student = new Student { Name = "小明", Age = 18, Address = "北京市海淀区" }; PropertyInfo propertyInfo = typeof(Student).GetProperty("Name"); propertyInfo.SetValue(student, "小红"); Console.WriteLine("Name is {0}", student.Name); }}class Student{ public string Name { get; set; } public int Age { get; set; } public string Address { get; set; }}输出结果:
Name is 小红下面是一个示例,使用反射更新对象的属性值。
class Program{ static void Main() { Student student = new Student { Name = "小明", Age = 18, Address = "北京市海淀区" }; Type type = typeof(Student); PropertyInfo nameProperty = type.GetProperty("Name"); nameProperty.SetValue(student, "小红"); PropertyInfo ageProperty = type.GetProperty("Age"); ageProperty.SetValue(student, 20); Console.WriteLine("Name is {0}, Age is {1}", student.Name, student.Age); }}class Student{ public string Name { get; set; } public int Age { get; set; } public string Address { get; set; }}输出结果:
Name is 小红, Age is 20下面是一个示例,使用反射动态创建对象并设置属性值。
class Program{ static void Main() { Type type = typeof(Student); object student = Activator.CreateInstance(type); PropertyInfo nameProperty = type.GetProperty("Name"); nameProperty.SetValue(student, "小红"); PropertyInfo ageProperty = type.GetProperty("Age"); ageProperty.SetValue(student, 20); PropertyInfo addressProperty = type.GetProperty("Address"); addressProperty.SetValue(student, "北京市海淀区"); Console.WriteLine("Name is {0}, Age is {1}, Address is {2}", nameProperty.GetValue(student), ageProperty.GetValue(student), addressProperty.GetValue(student)); }}class Student{ public string Name { get; set; } public int Age { get; set; } public string Address { get; set; }}输出结果:
Name is 小红, Age is 20, Address is 北京市海淀区本文详细讲解了C# PropertyInfo类的使用方法和示例,其中包括获取属性信息、获取属性值、设置属性值等部分。PropertyValue类可以很方便地使用反射来操作对象的属性,动态创建对象、更新对象的属性等等。对于使用C#反射的开发者,PropertyValue类的掌握必不可少。