using System.Reflection; static void Main(string[] args) { TestReflection(); } //------------------------------------------------------ // 反射(Reflection) public class RefClass { public int age { set; get; } } public static void TestReflection() { Type t = typeof(RefClass); RefClass rc = new RefClass(); //rc.age = 35; FieldInfo[] info = t.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly); foreach (FieldInfo item in info) { // 属性赋值 item.SetValue(rc, 35); Console.WriteLine("字段名称:\t" + item.Name + "\t字段类型:\t" + "rc中的值:\t" + item.GetValue(rc)); } Console.ReadLine(); }