所以我有一个 CategoryAttribute 类:
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Method, AllowMultiple = false)]
public class CategoryAttribute : PropertyAttribute
{
public readonly string name;
public readonly float order;
public CategoryAttribute(string name, float order)
{
this.name = name;
this.order = order;
}
}
这样我就可以对我的类(class)成员进行分类,以便在检查器 (Unity3D) 中分组绘制,如下所示:
现在这些是默认组(顺序为 0 的“字段”、顺序为 1 的“属性”和顺序为 2 的“方法”)。
现在我想让用户用他们自己的名字和订单创建他们自己的类别,所以说我想创建一个“调试”类别:
[Category("Debug", 3)]
public bool debugMode;
[Category("Debug", 3)]
public bool drawGizmos;
[Category("Debug", 3)]
public Color gizmosColor;
现在这三个字段都将被绘制在“方法”之后的一个名为“调试”的类别中(因为它具有更高的顺序)(我知道语法是重复的,我稍后会做一些事情......)
现在我似乎无法按类别名称对我的成员进行分组,也无法按该类别的订单号对这些组进行排序,我试过了但遇到了问题:
var members = fields.Concat<MemberInfo>(properties).Concat(methods);
var userCategories = from m in members
let cat = m.GetCustomAttributes(typeof(CategoryAttribute), false)[0]
where attr != null
let cat = attr as CategoryAttribute
group m by cat.name into g
orderby cat.order // bump!
和:
var userCategories = members.Select(m => new {m, cat = m.GetCustomAttributes(typeof(CategoryAttribute), false)[0] as CategoryAttribute})
.Where(x => x.cat != null)
.GroupBy(x => x.cat.name)
.OrderBy(x => x.cat.order) // duh!
现在我可以摆弄预测并最终弄清楚,但这可能不是最好的,所以我想也许有人可以帮助我以正确的方式做到这一点。
同样,我想按类别名称对我的成员进行分组,并按类别编号对组进行排序。
谢谢!
请您参考如下方法:
如果这是在 LINQ to Objects 中,您可以只使用:
.OrderBy(g => x.First().cat.order)
或者,您可以按名称和顺序进行分组,然后使用键的顺序部分:
.GroupBy(x => new { x.cat.name, x.cat.order })
.OrderBy(g => g.Key.order)
基本上,您需要了解在 GroupBy 之后您没有可以请求类别的单个项目 - 您有一组项目。因此,您需要询问与另一组相比,您如何对该组进行排序。
