问题描述
我需要从我项目中的任何枚举
创建一个 SelectList
我有下面的代码,我从一个特定的枚举中创建一个选择列表,但是我想为任何枚举设置一个扩展方法。此示例检索每个枚举值的 DescriptionAttribute
的值
var list = new SelectList(
Enum.GetValues(typeof(eChargeType))
.Cast< eChargeType>()
.Select(n => new
{
id =(int)n,
label = n.ToString()
}),id,label,charge.type_id);参考,如何继续? public static void ToSelectList(this Enum e)
{
//这里的代码
}
解决方案 p>我认为你正在努力,是描述的检索。我确定一旦你有了那些你可以定义最终的方法,给出你的确切的结果。
首先,如果你定义一个扩展方法,它的作用是一个值的枚举,而不是枚举类型本身。我想,为了方便使用,您可以调用类型的方法(如静态方法)。不幸的是,您无法定义这些。
您可以做的是以下几点。首先定义一个检索枚举值描述的方法,如果它有一个:
public static string GetDescription(此枚举值){
string description = value.ToString();
FieldInfo fieldInfo = value.GetType()。GetField(description);
DescriptionAttribute [] attributes =(DescriptionAttribute [])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute),false);
if(attributes!= null&& attributes.Length> 0){
description = attributes [0] .Description;
}
返回描述;
}
接下来,定义一个采用枚举的所有值的方法,并使用以前的方法来查找我们要显示的值,并返回该列表。可以推断通用参数。
public static List< KeyValuePair< TEnum,string>> ToEnumDescriptionsList< TEnum>(此TEnum值){
返回枚举
.GetValues(typeof(TEnum))
.Cast< TEnum>()
.Select(x => new KeyValuePair< TEnum,string>(x,((Enum)((object)x))GetDescription()))
.ToList();
}
最后,为了方便使用,直接调用它的方法没有价值。但是,通用参数不是可选的。
public static List< KeyValuePair< TEnum,string>> ToEnumDescriptionsList< TEnum>(){
return ToEnumDescriptionsList< TEnum>(default(TEnum));
}
现在我们可以这样使用:
枚举TestEnum {
[描述(我的第一个值)]
Value1,
Value2,
[描述(最后一个)]
Value99
}
var items = default(TestEnum).ToEnumDescriptionsList();
//或:TestEnum.Value1.ToEnumDescriptionsList();
//替代方法:EnumExtensions.ToEnumDescriptionsList< TestEnum>()
foreach(项目中的项目){
Console.WriteLine({0} - {1},item.Key, item.Value);
}
Console.ReadLine();
哪些输出:
Value1 - 我的第一个值
Value2 - Value2
Value99 - 最后一个
I need to create a SelectList
from any Enum
in my project.
I have the code below which I create a select list from a specific enum, but I'd like to make an extension method for ANY enum. This example retrieves the value of the DescriptionAttribute
on each Enum value
var list = new SelectList(
Enum.GetValues(typeof(eChargeType))
.Cast<eChargeType>()
.Select(n => new
{
id = (int)n,
label = n.ToString()
}), "id", "label", charge.type_id);
Referencing this post, how do I proceed?
public static void ToSelectList(this Enum e)
{
// code here
}
解决方案 What I think you are struggling with, is the retrieval of the description. I'm sure once you have those that you can define your final method which gives your exact result.
First, if you define an extension method, it works on a value of the enum, not on the enum type itself. And I think, for easy of usage, you would like to call the method on the type (like a static method). Unfortunately, you cannot define those.
What you can do is the following. First define a method which retrieves the description of the enum value, if it has one:
public static string GetDescription(this Enum value) {
string description = value.ToString();
FieldInfo fieldInfo = value.GetType().GetField(description);
DescriptionAttribute[] attributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
if (attributes != null && attributes.Length > 0) {
description = attributes[0].Description;
}
return description;
}
Next, define a method which takes all values of an enum, and use the previous method to look up the value which we want to show, and return that list. The generic argument can be inferred.
public static List<KeyValuePair<TEnum, string>> ToEnumDescriptionsList<TEnum>(this TEnum value) {
return Enum
.GetValues(typeof(TEnum))
.Cast<TEnum>()
.Select(x => new KeyValuePair<TEnum, string>(x, ((Enum)((object)x)).GetDescription()))
.ToList();
}
And finally, for ease of usage, a method to call it directly without value. But then the generic argument is not optional.
public static List<KeyValuePair<TEnum, string>> ToEnumDescriptionsList<TEnum>() {
return ToEnumDescriptionsList<TEnum>(default(TEnum));
}
Now we can use it like this:
enum TestEnum {
[Description("My first value")]
Value1,
Value2,
[Description("Last one")]
Value99
}
var items = default(TestEnum).ToEnumDescriptionsList();
// or: TestEnum.Value1.ToEnumDescriptionsList();
// Alternative: EnumExtensions.ToEnumDescriptionsList<TestEnum>()
foreach (var item in items) {
Console.WriteLine("{0} - {1}", item.Key, item.Value);
}
Console.ReadLine();
Which outputs:
Value1 - My first value
Value2 - Value2
Value99 - Last one
这篇关于通用枚举到SelectList扩展方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!