如何转换GetWithOrder
方法以正确的顺序返回实际的枚举?它适用于枚举的字符串,但我想要实际的枚举值,当然还需要通用。
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]
public class EnumOrderAttribute : Attribute
{
public int Order { get; set; }
}
public static class EnumExtenstions
{
public static IEnumerable<string> GetWithOrder(this Enum enumVal)
{
return enumVal.GetType().GetWithOrder();
}
public static IEnumerable<string> GetWithOrder(this Type type)
{
if (!type.IsEnum)
{
throw new ArgumentException("Type must be an enum");
}
// caching for result could be useful
return type.GetFields()
.Where(field => field.IsStatic)
.Select(field => new
{
field,
attribute = field.GetCustomAttribute<EnumOrderAttribute>()
})
.Select(fieldInfo => new
{
name = fieldInfo.field.Name,
order = fieldInfo.attribute != null ? fieldInfo.attribute.Order : 0
})
.OrderBy(field => field.order)
.Select(field => field.name);
}
}
public enum TestEnum
{
[EnumOrder(Order=2)]
Second = 1,
[EnumOrder(Order=1)]
First = 4,
[EnumOrder(Order=3)]
Third = 0
}
var names = typeof(TestEnum).GetWithOrder();
var names = TestEnum.First.GetWithOrder();
答案 0 :(得分:4)
枚举类型变量的扩展方法没有意义,因为您不需要该变量的值 - 您只需要枚举类型。所以我会选择静态助手类:
public static class Enum<T>
{
public static IEnumerable<T> GetOrderedValues()
{
var type = typeof(T);
if (!type.IsEnum)
throw new InvalidOperationException();
return from f in type.GetFields(BindingFlags.Static | BindingFlags.Public)
orderby f.GetCustomAttribute<EnumOrderAttribute>()?.Order ?? Int32.MaxValue
select (T)f.GetValue(obj: null);
}
}
注意:枚举类型有一个非静态value
字段,它实际上保存枚举的值。因此,如果要列出枚举成员,则只需要采用静态字段。
用法
var values = Enum<Fruits>.GetOrderedValues();
样本枚举
public enum Fruits
{
Banana,
[EnumOrder(2)]
Apple,
[EnumOrder(1)]
Lemon
}
输出
[ Lemon, Apple, Banana ]
没有EnumOrder
属性的成员将排在最后,但如果您使用其他默认顺序而不是Int32.MaxValue
,则可以更改该行为。