C#反射设置和读取类的属性

C#反射设置和读取类的属性 命名空间引入 using System.Reflection; 首先定义一个类 public class Dog { public string DogName { get; set; } } 类的实例: Dog mydog=new Dog(); 获取实例类型 Type t = typeof(Dog); 或者Type t = mydog.GetType(); 有了这个类型t,就可以利用反射对这个实例为所欲为了: 设置属性值: t.GetProperty("DogName").SetValue(mydog, "pet", null); 获取属性值: t.GetProperty("DogName").GetValue(mydog, null).ToString();
原文地址:https://www.cnblogs.com/lmfeng/p/2109384.html