C#之反射(PropertyInfo类)


1、引入命名空间:System.Reflection;程序集:mscorlib(在mscorlib.dll中)

2、示例代码(主要是getType()、setValue()、getValue()方法):

  1 using System;  2 using System.Collections.Generic;  3 using System.Linq;  4 using System.Reflection;  5 using System.Text;  6 using System.Threading.Tasks;  7  8 namespace 反射  9 {  10 class Program  11  {  12 /// <summary>  13 /// 反射描述了程序集、模块、类型的对象(Type类型)。可以使用反射动态创建类型的实例,将实例绑定到现有对象,或从现有对象获取类型并调用其方法或访问其字段和属性  14 /// 如果代码中使用了特性,可以利用反射来访问它们  15 /// </summary>  16 static void Main(string[] args)  17  {  18  19 //1、GetType()的解释及示例  20 //  21 // 摘要:  22 // 获取当前实例的 System.Type。  23 //  24 // 返回结果:  25 // System.Type 实例,表示当前实例的确切运行时类型。  26  27 //下面使用静态方法GetType(从Object基类派生的所有类型都继承该方法)获取其类型的简单反射示例  28 Student stu1 = new Student() { id = 1, name = "张三", length = 175, datetime = DateTime.Now };//  29 Student stu2 = new Student() { id = 2, name = "李四", length = 180, datetime = DateTime.Now };//name = "李四", datetime = DateTime.Now  30  31 var list = new List<Student>();  32  list.Add(stu1);  33  list.Add(stu2);  34  35 Type stuType = stu1.GetType();  36 Type listType = list.GetType();  37  38 Console.WriteLine("---------------getType()输出类型----------------");  39 Console.WriteLine(stuType);//输出Student的类型  40 Console.WriteLine(stu1.id.GetType());//输出Student的id的类型  41 Console.WriteLine(stu1.name.GetType());//输出Student的name的类型  42 Console.WriteLine(stu1.datetime.GetType());//输出Student的datetime的类型  43  44 Console.WriteLine(listType);//输出List的类型  45  46  47 //2、GetProperties()的解释及示例  48 //  49 // 摘要:  50 // 返回当前 System.Type 的所有公共属性。  51 //  52 // 返回结果:  53 // 表示当前 System.Type 的所有公共属性的 System.Reflection.PropertyInfo 对象数组。- 或 -如果当前 System.Type  54 // 没有公共属性,则为 System.Reflection.PropertyInfo 类型的空数组。  55  56 PropertyInfo[] stuPropertyS = stuType.GetProperties();  57 PropertyInfo[] listPropertyS = listType.GetProperties();  58  59 //Console.WriteLine(stuPropertyS.Count());  60 //Console.WriteLine(stuPropertyS.Length);  61  62 //3、SetValue()的解释及示例  63 //  64 // 摘要:  65 // 用索引属性的可选索引值设置该属性的值。  66 //  67 // 参数:  68 // obj:  69 // 将设置其属性值的对象。  70 //  71 // value:  72 // 此属性的新值。  73 //  74 // index:  75 // 索引化属性的可选索引值。对于非索引化属性,此值应为 null。 76 // 77 // 异常: 78 // System.ArgumentException: 79 // index 数组不包含所需类型的参数。- 或 -未找到该属性的 set 访问器。 80 // 81 // System.Reflection.TargetException: 82 // 该对象与目标类型不匹配,或者某属性是实例属性但 obj 为 null。 83 // 84 // System.Reflection.TargetParameterCountException: 85 // index 中参数的数目与已编制索引的属性所采用的参数的数目不相符。 86 // 87 // System.MethodAccessException: 88 // 尝试非法访问某类中私有或受保护的方法。 89 // 90 // System.Reflection.TargetInvocationException: 91 // 设置属性值时出错。例如,为索引属性指定的索引值超出范围。System.Exception.InnerException 属性指示错误的原因。 92 Console.WriteLine("---------------setValue()给某属性赋值----------------"); 93 PropertyInfo stuPro = stuPropertyS.FirstOrDefault(x => x.Name == "name");//这里是找name的属性名 94 stuPro.SetValue(stu1, "王五", null); 95 Console.WriteLine(stu1.name); 96 97 98 //4、GetType()方法的解释和示例 99 // 100 // 摘要: 101 // 用索引化属性的可选索引值返回指定对象的该属性值。 102 // 103 // 参数: 104 // obj: 105 // 将返回其属性值的对象。 106 // 107 // index: 108 // 索引化属性的可选索引值。 对于非索引化属性,该值应为 null。 109 // 110 // 返回结果: 111 // 指定对象的属性值。 112 // 113 // 异常: 114 // System.ArgumentException: 115 // index 数组不包含所需类型的参数。 - 或 - 未找到该属性的 get 访问器。 116 // 117 // System.Reflection.TargetException: 118 // 该对象与目标类型不匹配,或者某属性是实例属性但 obj 为 null。 119 // 120 // System.Reflection.TargetParameterCountException: 121 // index 中参数的数目与已编制索引的属性所采用的参数的数目不相符。 122 // 123 // System.MethodAccessException: 124 // 尝试非法访问某类中私有或受保护的方法。 125 // 126 // System.Reflection.TargetInvocationException: 127 // 检索属性值时出错。 例如,为索引属性指定的索引值超出范围。 System.Exception.InnerException 属性指示错误的原因。 128 Console.WriteLine("---------------getValue()遍历属性值----------------"); 129 foreach (var v in stuPropertyS)//遍历属性 130 { 131 Console.WriteLine(v.GetValue(stu1, null)); 132 } 133 134 135 Console.ReadLine(); 136 } 137 } 138 139 public class Student 140 { 141 public int id { get; set; } 142 public string name { get; set; } 143 public decimal length { get; set; } 144 public DateTime datetime { get; set; } 145 } 146 }

3、运行结果:

4、下面是PropertyInfo 类型主要公开的成员,可供参考(摘自:http://msdn.microsoft.com/zh-cn/library/system.reflection.propertyinfo(v=vs.110).aspx


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM