Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 按customAttribute的值对对象的属性排序_C#_Linq_C# 4.0_Reflection_Linq To Objects - Fatal编程技术网

C# 按customAttribute的值对对象的属性排序

C# 按customAttribute的值对对象的属性排序,c#,linq,c#-4.0,reflection,linq-to-objects,C#,Linq,C# 4.0,Reflection,Linq To Objects,我想做的是wirte linq表达式,它允许我按自定义属性对某个对象的列表进行排序,例如: public class SampleClass{ [CustomAttribute("MyAttrib1",1)] public string Name{ get; set; } [CustomAttribute("MyAttrib2",1)] public string Desc{get;set;} [CustomAttribute("MyAttrib1",2)]

我想做的是wirte linq表达式,它允许我按自定义属性对某个对象的
列表进行排序,例如:

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());
输出列表应该是(我只能用PropertyInfo.Name告诉它):

property name: Name,Price,Desc
我的问题是:有可能做到吗?如果是,我如何才能正确执行此操作?

如果你有什么问题,请问(我会尽我所能回答每一个不确定性)。我希望对问题的描述足够了

感谢您的帮助:)

var props=typeof(SampleClass)
.GetProperties()
.OrderBy(p=>p.GetCustomAttributes().OfType().First().AttribName)
.ThenBy(p=>p.GetCustomAttributes().OfType().First().Index)
.选择(p=>p.Name);
var propNames=String.Join(“,”props);

输出:Name,Price,Desc

我之所以读这篇文章,是因为我想查看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);

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

我的意思是按属性的顺序输出
列表
。名称:),而您的解决方案不起作用
p.GetCustomAttributes()
需要参数
bool
当我写同样的东西时,VS2010无法在
p.GetCustomAttributes()之后找到任何方法,因为没有
GetCustomAttributes()的无参数方法
当我像这样在方法中添加参数时
GetCustomAttributes(false)
您的答案有效:)非常感谢:)
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); 
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);