【问题标题】:Order properties of object by value of customAttribute按 customAttribute 的值排序对象的属性
【发布时间】:2012-10-19 00:10:22
【问题描述】:

我正在尝试做的是 wirte linq 表达式,它允许我通过自定义属性订购我的某些对象的 List<PropertyInfo>,例如:

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

我的问题是:可以这样做吗?如果是的话,我该如何正确地做到这一点?

如果您有任何问题,请提出(我会尽力回答所有不确定性)。我希望对问题的描述就足够了。

感谢您的提前:)

【问题讨论】:

    标签: c# linq c#-4.0 reflection linq-to-objects


    【解决方案1】:
    var props = typeof(SampleClass)
        .GetProperties()
        .OrderBy(p => p.GetCustomAttributes().OfType<CustomAttribute>().First().AttribName)
        .ThenBy(p => p.GetCustomAttributes().OfType<CustomAttribute>().First().Index)
        .Select(p => p.Name);
    
    var propNames = String.Join(", ", props); 
    

    输出:名称、价格、描述

    【讨论】:

    • 我的意思是按属性的顺序输出List&lt;PropertyInfo&gt; :) 而你的解决方案不起作用p.GetCustomAttributes() 需要参数bool
    • 当我写同样的东西时,VS2010 在p.GetCustomAttributes() 之后无法找出任何方法,因为没有GetCustomAttributes() 的无参数方法
    • 当我在 GetCustomAttributes(false) 这样的方法中添加参数时,您的回答工作:) 非常感谢 :)
    【解决方案2】:

    我正在阅读这篇文章,因为我想查看 xml 属性并遇到了错误,因为似乎有了这个答案,我也得到了我不想要的属性。然后我做了一些似乎有用的东西,我对自己说,它总有一天会帮助别人。

    IOrderedEnumerable<PropertyInfo> Properties = typeof(FooClass).GetProperties()
                    .Where(p => p.CustomAttributes.Any(y => y.AttributeType == typeof(XmlElementAttribute)))
                    .OrderBy(g=> g.GetCustomAttributes(false).OfType<XmlElementAttribute>().First().Order);
    
    

    【讨论】:

      猜你喜欢
      • 2011-01-28
      • 1970-01-01
      • 2016-02-08
      • 2013-02-12
      • 2023-04-03
      • 2021-07-20
      • 2010-12-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多