开发者

How do I sort a generic list based on a custom attribute?

开发者 https://www.devze.com 2022-12-19 12:33 出处:网络
I am workin开发者_StackOverflow社区g in c#.NEt 2.0. I have a class, let\'s say X with many properties. Each properties has a custom attribute, an interger, which I planned to use to specify its order

I am workin开发者_StackOverflow社区g in c#.NEt 2.0. I have a class, let's say X with many properties. Each properties has a custom attribute, an interger, which I planned to use to specify its order int he final array.

Using reflection I read through all of the properties and group the values and place them into a generic list of properties. This works and I can grab the values. But the plan was SORT the list, based on the custom attribute placed on each property, and finally read out the propery values, already ordered, into a string.


Lets say you had the following attribute definition

public class SortAttribute : Attribute { 
  public int Order { get; set; }
  public SortAttribute(int order) { Order = order; }
}

You could use the following code to pull out the properties on a type in sorted order. Assuming of course they all have this attribute

public IEnumerable<object> GetPropertiesSorted(object obj) {
  Type type = obj.GetType();
  List<KeyValuePair<object,int>> list = new List<KeyValuePair<object,int>>();
  foreach ( PropertyInfo info in type.GetProperties()) {
    object value = info.GetValue(obj,null);
    SortAttribute sort = (SortAttribute)Attribute.GetCustomAttribute(x, typeof(SortAttribute), false);
    list.Add(new KeyValuePair<object,int>(value,sort.Order));
  }
  list.Sort(delegate (KeyValuePair<object,int> left, KeyValuePair<object,int> right) { left.Value.CompareTo right.Value; });
  List<object> retList = new List<object>();
  foreach ( var item in list ) {
    retList.Add(item.Key);
  }
  return retList;
}

LINQ Style Solution

public IEnumerable<string> GetPropertiesSorted(object obj) {
  var type = obj.GetType();
  return type
    .GetProperties()
    .Select(x => new { 
      Value = x.GetValue(obj,null),
      Attribute = (SortAttribute)Attribute.GetCustomAttribute(x, typeof(SortAttribute), false) })
    .OrderBy(x => x.Attribute.Order)
    .Select(x => x.Value)
    .Cast<string>();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消