【发布时间】:2017-01-18 04:51:43
【问题描述】:
我想根据给定的属性参数Order 对属性进行排序。
属性:
public class Items: Attribute
{
public int Order { get; set; }
public Items(int order)
{
this.Order = order;
}
}
实施:
public class client
{
[Items(1)]
public string fName {get; set;}
[Items(3)]
public string lName {get; set;}
[Items(2)]
public string mName {get; set;}
}
获取所有属性:
var properties = typeof(client).GetProperties().Where(
prop => prop.IsDefined(typeof(Items), false));
按顺序#排序?
这是我尝试过的,但它不起作用
Array.Sort(properties, delegate (Items x, Items y)
{ return x.Order.CompareTo(y.Order); });
如何根据订单对属性进行排序?
这个问题已经解决了,但是想扩展这个问题。
Is there a way to sort properties without having to put an "Order" on them。我只想有一个属性“EndOfList”或“Last”来说明一定要对这些最后进行排序。这样我就不必用 Orders 把代码弄得乱七八糟了。
【问题讨论】:
标签: c# sorting properties attributes