Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.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# 如何向属性添加属性并使用它对成员进行排序_C# - Fatal编程技术网

C# 如何向属性添加属性并使用它对成员进行排序

C# 如何向属性添加属性并使用它对成员进行排序,c#,C#,如何使用某种order属性标记属性,以帮助我按顺序遍历属性,从而读取带分隔符的文件 这些类是用每个列的属性创建的,我想编写一个例程来处理它,而不是手工操作 比如: [Order(1)] public string Field1 { get; set; } [Order(2)] public string Field2 { get; set; } MemberInfo[] myMembers = myType.GetMembers() .OrderBy(something here o

如何使用某种order属性标记属性,以帮助我按顺序遍历属性,从而读取带分隔符的文件

这些类是用每个列的属性创建的,我想编写一个例程来处理它,而不是手工操作

比如:

[Order(1)]
public string Field1 { get; set; }

[Order(2)]
public string Field2 { get; set; }
MemberInfo[] myMembers = myType.GetMembers()
    .OrderBy(something here or in a loop);
然后是:

[Order(1)]
public string Field1 { get; set; }

[Order(2)]
public string Field2 { get; set; }
MemberInfo[] myMembers = myType.GetMembers()
    .OrderBy(something here or in a loop);
有什么想法吗?

使用
MemberInfo
上的属性获取
OrderAttribute
并为排序选择它的值

var members = myType.GetMembers()
                    .OrderBy(m => m.CustomAttributes
                                   .OfType(typeof(OrderAttribute))
                                   .FirstOrDefault(a => a.Value))

您需要一个OrderAttribute类:

public class OrderAttribute : Attribute
{
    public OrderAttribute(int value)
    {
        this.Value = value;
    }

    public int Value { get; private set; }
}
然后,您可以按以下属性进行排序:

MemberInfo[] myMembers = typeof(object).GetMembers()
    .OrderBy(m => m.GetCustomAttribute<OrderAttribute>().Value);
MemberInfo[]myMembers=typeof(object).GetMembers()
.OrderBy(m=>m.GetCustomAttribute().Value);

这只是一个基本的示例,我建议您检查您的成员是否确实具有此属性,如果没有,则抛出一个适当的错误,或者假设您建议的默认值是可行的

创建一个属性:

public class OrderAttribute : Attribute
{
    public int Order { get; private set; }
    public OrderAttribute(int order)
    {
        Order = order;
    }
}
然后在OrderBy调用中通过反射获取OrderAttribute.Order字段。 最好将该逻辑放在它自己的方法中,或者在PropertyInfo上创建一个扩展方法

所以看起来是这样的:

MemberInfo[] myMembers = myType.GetMembers()
.OrderBy(m => m.GetAttribute<OrderAttribute>().Order);
MemberInfo[]myMembers=myType.GetMembers()
.OrderBy(m=>m.GetAttribute().Order);
我创建了一个通用扩展方法来处理属性检索

    public static TAttribute GetAttribute<TAttribute>(this object obj) 
        where TAttribute : Attribute
    {
        return obj.GetType().GetAttribute<TAttribute>();
    }


    public static TAttribute GetAttribute<TAttribute>(this ICustomAttributeProvider propInfo, bool inherit = true) 
        where TAttribute : Attribute
    {
        return (TAttribute)propInfo.GetCustomAttributes(typeof(TAttribute), inherit).FirstOrDefault();
    }
publicstatAttribute-GetAttribute(此对象对象对象)
其中:属性
{
返回obj.GetType().GetAttribute();
}
公共静态TatAttribute GetAttribute(此ICCustomAttributeProvider propInfo,bool inherit=true)
其中:属性
{
return(tatAttribute)propInfo.GetCustomAttributes(typeof(tatAttribute),inherit.FirstOrDefault();
}

我会这样做:

var members = myType.GetMembers()
                .Where(m => Attribute.IsDefined(m, typeof(OrderAttribute)))
                .OrderBy(m => m.GetCustomAttribute<OrderAttribute>().Order);

如果其中一个成员没有
OrderAttribute
谢谢,我刚才在您的评论中添加了一个注释@MartinBooth,谢谢,这正是我想要的。如果有人不使用.net framework 4.5,那么您的扩展方法肯定很有用!看看这些内置的扩展方法吧,不过如果你愿意的话,谢谢Martin,我现在很高兴了解它在.net框架中的作用谢谢Martin。绝对有用的信息。