私がやろうとしているのはList<PropertyInfo>
、たとえば次のようなカスタム属性でオブジェクトを並べ替えることができるwirtelinq式です。
public class SampleClass{
[CustomAttribute("MyAttrib1",1)]
public string Name{ get; set; }
[CustomAttribute("MyAttrib2",1)]
public string Desc{get;set;}
[CustomAttribute("MyAttrib1",2)]
public int Price{get;set;}
}
CustomAttribute.cs:
public class CustomAttribute: Attribute{
public string AttribName{get;set;}
public int Index{get;set;}
public CustomAttribute(string attribName,int index)
{
AttribName = attribName;
Index = index;
}
}
これまでのところ、SampleClassという名前のクラスからすべてのプロパティを取得できました。
List<PropertyInfo> propertiesList = new List<PropertyInfo>((IEnumerable<PropertyInfo>)typeof(SampleClass).GetProperties());
私はこれまでこれをソートしようとしましたpropertiesList
(ところで、これは機能しません):
var sortedPropertys = propertiesList
.OrderByDescending(
(x, y) => ((CustomAttribute) Attribute.GetCustomAttribute((PropertyInfo) x, typeof (CustomAttribute))).AttribName
.CompareTo((((CustomAttribute) Attribute.GetCustomAttribute((PropertyInfo) y, typeof (CustomAttribute))).AttribName ))
).OrderByDescending(
(x,y)=>((CustomAttribute) Attribute.GetCustomAttribute((PropertyInfo) x, typeof (CustomAttribute))).Index
.CompareTo((((CustomAttribute) Attribute.GetCustomAttribute((PropertyInfo) y, typeof (CustomAttribute))).Index)))
.Select(x=>x);
出力リストは次のようになります(PropertyInfo.Nameでのみ通知します):
property name: Name,Price,Desc
私の質問は:それを行うことは可能ですか?はいの場合、どうすればこれを適切に行うことができますか?
あなたがいくつかの質問をするならば、plsは尋ねます(私はすべての不確実性に答えるために最善を尽くします)。問題の説明で十分だと思います。
よろしくお願いします:)