C#遍历类的属性 PropertyInfo.Attributes
来源:互联网 发布:淘宝客寻找内部优惠券 编辑:程序博客网 时间:2024/05/20 07:17
PropertyInfo.Attributes 属性
此属性表示与成员关联的特性。 所有成员都具有相对于特定成员类型定义的特性集。 属性特性使用户能够知道此属性是否是默认属性、SpecialName 属性等等。
若要获取 Attributes 属性,请先获取类类型。 从 Type 获取 PropertyInfo。 从 PropertyInfo 获取特性。
官方示例:获取类的特性
1 using System; 2 using System.Reflection; 3 4 public class Myproperty 5 { 6 private string caption = "Default caption"; 7 public string Caption 8 { 9 get{return caption;}10 set {if(caption!=value) {caption = value;}11 }12 }13 }14 15 class Mypropertyinfo16 {17 public static int Main(string[] args)18 {19 Console.WriteLine("\nReflection.PropertyInfo");20 21 // Define a property.22 Myproperty Myproperty = new Myproperty();23 Console.Write("\nMyproperty.Caption = " + Myproperty.Caption);24 25 // Get the type and PropertyInfo.26 Type MyType = Type.GetType("Myproperty");27 PropertyInfo Mypropertyinfo = MyType.GetProperty("Caption");28 29 // Get and display the attributes property.30 PropertyAttributes Myattributes = Mypropertyinfo.Attributes;31 32 Console.Write("\nPropertyAttributes - " + Myattributes.ToString());33 34 return 0;35 }36 }
官方参考:http://msdn.microsoft.com/zh-cn/library/system.reflection.propertyinfo.attributes
一个例子: 注意:貌似对字段无效
先建一个类User
1 namespace ClassLibrary1 2 { 3 public class User 4 { 5 private int userid = 1; 6 public int Userid 7 { 8 get { return userid; } 9 set { userid = value; }10 } 11 private string userName = "jghg"; 12 public string UserName{13 get { return userName; } 14 set { userName = value; }15 }16 private string address = "ghjghj";17 public string Address{ 18 get { return address; } 19 set { address = value; }20 } 21 private string email = "jhgjhg";22 public string Email{23 get { return email; }24 set { email = value; }25 }26 private string phone = "ghjgjg";27 public string Phone28 { 29 get { return phone; }30 set { phone = value; }31 }32 }33 }
接着在主程序中获取类的属性,看代码
1 namespace ConsoleApplication2 { 2 class Program { 3 static void Main(string[] args) 4 { 5 Type type = typeof(ClassLibrary1.User); 6 object obj = Activator.CreateInstance(type); 7 PropertyInfo[] props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); 8 foreach (PropertyInfo p in props) { 9 Console.WriteLine(p.Name); 10 } 11 Console.ReadLine(); 12 }13 } 14 }
需要引入命名空间:
using System.Reflection;
0 0
- C#遍历类的属性 PropertyInfo.Attributes
- C#遍历类的属性 PropertyInfo.Attributes
- C#遍历类的属性 PropertyInfo.Attributes
- C#遍历类的属性 PropertyInfo.Attributes
- C#根据字符串得到静态类的属性(PropertyInfo)
- C#中属性PropertyInfo的使用
- C#中属性PropertyInfo的使用
- 属性PropertyInfo的使用
- C# PropertyInfo的用法
- C#利用PropertyInfo反射类实现二个类属性值的比较
- C#利用PropertyInfo反射类实现二个类属性值的比较
- C#中属性PropertyInfo的使用,Dictionary转为Model实例
- C#遍历类的所有属性
- C#类的属性遍历及属性值获取
- button控件的.Attributes属性
- 关于Attributes属性的介绍
- 文本属性Attributes的用法
- C#中Attributes的用法
- 第13周项目5-拓扑排序算法的验证
- 第十五周项目二~~用哈希法组织关键字
- 设计模式-深入解析观察者模式(Observer)
- mvc:view-controller
- 第十五周 项目1-验证算法(1)
- C#遍历类的属性 PropertyInfo.Attributes
- 第二篇 虚拟机性能监控与故障处理工具(JDK的命令行工具/JDK的可视化工具)
- Java常见异常总结
- 第十五周--验证算法
- BlockingQueue的使用
- TabLayout 修改字体的方法
- 12招婚礼配色技巧 帮你选择你最心仪的布置
- 第十二周项目1 图基本算法库
- hibernate持久化实现——数据缓存