GenericEnum转SelectList扩展方法我需要在项目中任意一个Enum中创建一个SelectList。我在下面的代码中从特定枚举创建了一个选择列表,但我想为任何枚举创建一个扩展方法。此示例检索每个枚举值的DescriptionAttribute值varlist=newSelectList(Enum.GetValues(typeof(eChargeType)).Cast().Select(n=>new{id=(int)n,label=n.ToString()}),"id","label",charge.type_id);参考这篇文章,我该如何处理?publicstaticvoidToSelectList(thisEnume){//codehere}我认为你正在努力的是检索描述。我相信一旦你有了这些,你就可以根据你的确切结果定义你的最终方法。首先,如果你定义了一个扩展方法,它将使用枚举的值,而不是枚举类型本身。我认为,为了便于使用,您想在类型上调用方法(如静态方法)。不幸的是,您无法定义这些。您可以执行以下操作。首先定义一个方法来检索枚举值的描述??,如果它有的话:publicstaticstringGetDescription(thisEnumvalue){stringdescription=value.ToString();FieldInfofieldInfo=value.GetType().GetField(描述);DescriptionAttribute[]attributes=(DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute),false);如果(属性!=null&&attributes.Length>0){description=attributes[0].Description;}返回描述;}接下来,定义一个方法获取枚举的所有值,并使用前面的方法找到我们要显示的值,并返回列表。可以推断泛型参数。publicstaticList>ToEnumDescriptionsList(这个TEnum值){returnEnum.GetValues(typeof(TEnum)).Cast().Select(x=>newKeyValuePair(x,((Enum)((object)x)).GetDescription())).ToList();最后,为了方便,一个不需要直接调用的方法。但是泛型参数不是可选的。公共静态列表>ToEnumDescriptionsList(){returnToEnumDescriptionsList(default(TEnum));现在我们可以这样使用它:enumTestEnum{[Description("Myfirstvalue")]Value1,Value2,[Description("Lastone")]Value99}varitems=default(TestEnum).ToEnumDescriptionsList();//或:TestEnum.Value1.ToEnumDescriptionsList();//备选方案:EnumExtensions.ToEnumDescriptionsList()foreach(variteminitems){Console.WriteLine("{0}-{1}",item.Key,item.Value);}控制台.ReadLine();哪些输出:Value1-我的第一个值Value2-Value2Value99-最后一个迟到,但由于没有公认的答案,它可能会帮助其他人:正如@Maarten提到的,扩展方法适用于枚举的值,而不是枚举类型itelf,所以在Maarteen的灵魂中,您可以创建一个虚拟值或默认值来调用扩展方法,但是,您可能会像我一样发现,使用这样的静态辅助方法更简单:publicstaticclassEnumHelper{publicstaticSelectListGetSelectList(stringselectedValue,booluseNumeric=false){类型enumType=GetBaseType(typeof(T));如果(enumType.IsEnum){varlist=newList();//添加空选项列表。Add(newSelectListItem{Value=string.Empty,Text=string.Empty});foreach(EnumeinEnum.GetValues(enumType)){list.Add(newSelectListItem{Value=useNumeric?Convert.ToInt32(e).ToString():e.ToString(),Text=e.Description()});}returnnewSelectList(list,"Value","Text",selectedValue);}返回空值;}privatestaticboolIsTypeNullable(Typetype){return(type.IsGenericType&&type.GetGenericTypeDefinition()==typeof(Nullable));}privatestaticTypeGetBaseType(类型类型){返回IsTypeNullable(类型)?type.GetGenericArguments()[0]:类型;您可以像这样创建一个选择列表:viewModel.ProvinceSelect=EnumHelper.GetSelectList(model.Province);或者使用可选的数值代替字符串:viewModel.MonthSelect=EnumHelper.GetSelectList(model.Month,true);我从这里得到了基本的想法,虽然我改变了它满足我的需要我添加的一件事是使用int值的选项。我还添加了一个枚举扩展来获取基于此博客文章的描述属性:publicstaticclassEnumExtensions{publicstaticstringDescription(thisEnumen){Typetype=en.GetType();MemberInfo[]memInfo=type.GetMember(en.ToString());如果(memInfo!=null&&memInfo.Length>0){object[]attrs=memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute),false);if(attrs!=null&&attrs.Length>0){return((DescriptionAttribute)attrs[0]).Description;}}返回en.ToString();由于枚举无法修复整个集合的扩展,因此扩展Enum基类的一种便捷方法是使用静态类型类。这将允许简洁的代码,例如:Enum.GetSelectItems();这可以通过以下方式实现:publicstaticclassEnum{publicstaticSelectListItem[]GetSelectItems(){Typetype=typeof(T);returnEnum.GetValues(type).Cast().Select(v=>newSelectListItem(){Value=v.ToString(),Text=Enum.GetName(type,v)}).ToArray();由于枚举不共享接口,因此可以使用类型滥用,但类名Enum应该可以消除任何混淆。这是Nathaniels答案的更正[typecasttoint]和简化[usetostringoverrideinsteadofgetname]版本,返回列表而不是数组:以上是C#学习教程:通用枚举到SelectList扩展方法共享的所有内容,如果它对大家有用,需要了解更多C#学习教程,希望大家多多关注——publicstaticclassEnum{//usage:varlst=Enum.GetSelectList();publicstaticListGetSelectList(){returnEnum.GetValues(typeof(T)).Cast().Select(i=>newSelectListItem(){Value=((int)i).ToString(),Text=i.ToString()}).ToList();}}本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
