2
如何將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();
只是想知道 - 爲什麼你會有沒有按字母順序或按價值排序的枚舉成員的順序? –
也許是因爲它們存儲在數據庫中,並且添加新的枚舉可能需要在顯示時按特定順序放置,但與存儲的值無關。 –
把'EnumOrderAttribute'放在'Enum'值上並且在迭代Enum.GetValues()'時檢查它是否更有意義? – DiskJunky