【发布时间】:2011-08-03 06:16:44
【问题描述】:
我正在尝试实现一个简单的 API,用户可以在其中使用属性属性来指定对象属性的排序。
类似:
[Sorting(SortOrder=0)]
public string Id { get; set; }
在基本的 ToString() 方法中,然后我使用反射从对象中提取属性。
Type currentType = this.GetType();
PropertyInfo[] propertyInfoArray = currentType.GetProperties(BindingFlags.Public);
Array.Sort(propertyInfoArray, this.comparer);
我已经使用 IComparer 接口编写了一个自定义类来执行 Array.Sort,但是一旦我进入其中,我就会在尝试检索 [Sorting] 属性时遇到困难。我目前有这样的东西:
PropertyInfo xInfo = (PropertyInfo)x;
PropertyInfo yInfo = (PropertyInfo)y;
我以为我可以使用 xInfo.Attributes,但 PropertyAttributes 类并没有做我需要它做的事情。有人对如何检索该 [排序] 属性有任何指导吗?我环顾四周,但由于“属性”一词在编程中的重载,我不断得到很多错误的线索和死胡同。
【问题讨论】:
标签: c# reflection attributes properties