有没有一种方法可以对属性进行排序,而不必下达“订单".在他们? [英] Is there a way to sort properties without having to put an "Order" on them?

查看:82
本文介绍了有没有一种方法可以对属性进行排序,而不必下达“订单".在他们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们已经弄清楚了如何在给定订单" 属性.

We have figured out how to sort properties given an "Order" attribute.

但是有一种方法可以对属性进行排序,而不必在属性上添加顺序".我只想拥有一个"EndOfList"或"Last"属性,该属性将确保对这些属性进行最后排序.这样我就不必用Orders弄乱代码了.

But is there a way to sort properties without having to put an "Order" on them. I am wanting to just have an attribute "EndOfList" Or "Last" that would state be sure to sort these last. So that I would not have to clutter up the code with Orders.

推荐答案

好,我知道了.

我们可以更改Attribute类.

public class Items : Attribute
{
    public bool Last { get; set; }
    public Items(bool last = false)
    {
        this.Last = last;
    }
}

然后更改主类

public class client
{
    [Items]
    public string fName { get; set; }

    [Items(true)]
    public string lName { get; set; }

    [Items]
    public string mName { get; set; }
}

然后更改排序

var sorted = properties
.OrderBy(p => ((Items)p.GetCustomAttributes(true)
                       .FirstOrDefault(a => a is Items)).Last);

这篇关于有没有一种方法可以对属性进行排序,而不必下达“订单".在他们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆