C# 使用扩展从枚举填充组合框?
目前,我使用以下扩展从枚举中读取C# 使用扩展从枚举填充组合框?,c#,winforms,lambda,C#,Winforms,Lambda,目前,我使用以下扩展从枚举中读取Description属性: public static T GetAttributeOfType<T>(this Enum enumVal) where T : System.Attribute { var type = enumVal.GetType(); var memInfo = type.GetMember(enumVal.ToString()); var attributes = memInfo[0].GetCust
Description
属性:
public static T GetAttributeOfType<T>(this Enum enumVal) where T : System.Attribute
{
var type = enumVal.GetType();
var memInfo = type.GetMember(enumVal.ToString());
var attributes = memInfo[0].GetCustomAttributes(typeof(T), false);
return (attributes.Length > 0) ? (T)attributes[0] : null;
}
var list = Enum.GetValues(typeof(MyEnum))
.Cast<MyEnum>()
.ToDictionary(k => k, v => v.GetAttributeOfType<DescriptionAttribute>().Description)
.ToList();
cbo.DataSource = list;
cbo.DisplayMember = "Value";
cbo.ValueMember = "Key";
public static T GetAttributeOfType(此枚举枚举值),其中T:System.Attribute
{
var type=enumVal.GetType();
var memInfo=type.GetMember(enumVal.ToString());
var attributes=memInfo[0].GetCustomAttributes(typeof(T),false);
返回(attributes.Length>0)?(T)属性[0]:空;
}
然后,在我的各种形式中,我使用以下代码从枚举填充组合框:
public static T GetAttributeOfType<T>(this Enum enumVal) where T : System.Attribute
{
var type = enumVal.GetType();
var memInfo = type.GetMember(enumVal.ToString());
var attributes = memInfo[0].GetCustomAttributes(typeof(T), false);
return (attributes.Length > 0) ? (T)attributes[0] : null;
}
var list = Enum.GetValues(typeof(MyEnum))
.Cast<MyEnum>()
.ToDictionary(k => k, v => v.GetAttributeOfType<DescriptionAttribute>().Description)
.ToList();
cbo.DataSource = list;
cbo.DisplayMember = "Value";
cbo.ValueMember = "Key";
var list=Enum.GetValues(typeof(MyEnum))
.Cast()
.ToDictionary(k=>k,v=>v.GetAttributeOfType().Description)
.ToList();
cbo.DataSource=列表;
cbo.DisplayMember=“Value”;
cbo.ValueMember=“Key”;
上面的代码运行良好。但是,我想在第二个扩展方法中移动代码,类似于:
public static Dictionary<T, string> FromEnum<T>(this ComboBox cbo) where T: struct, IConvertible
{
var list = Enum.GetValues(typeof(T))
.Cast<T>()
.ToDictionary(k => k, v => v.GetAttributeOfType<DescriptionAttribute>().Description)
.ToList();
cbo.DataSource = list;
cbo.DisplayMember = "Value";
cbo.ValueMember = "Key";
}
publicstaticdictionary fromnum(这个组合框cbo),其中T:struct,IConvertible
{
var list=Enum.GetValues(typeof(T))
.Cast()
.ToDictionary(k=>k,v=>v.GetAttributeOfType().Description)
.ToList();
cbo.DataSource=列表;
cbo.DisplayMember=“Value”;
cbo.ValueMember=“Key”;
}
但是上面的代码没有编译。我得到以下错误:
无法将lambda表达式转换为类型“System.Collections.Generic.IEqualityComparer”,因为它不是委托类型
及
找不到类型或命名空间名称“DescriptionAttribute”(是否缺少using指令或程序集引用?)
第二种扩展方法是否可以正常工作?您可以使用以下代码:
public enum SampleEnum
{
[Description("Some Value")]
First = 1,
[Description("Some Value")]
Second = 2,
[Description("Some Value")]
Third = 3,
}
public static IList<ListItem> ToSelectList<T>(this Enum @enum, string firstOption = null)
{
var items = Enum.GetValues(typeof(T)).Cast<T>().ToList<T>();
var list = items.Select(x => new ListItem() { Text = Enum.Parse(typeof(T), x.ToString()).GetDescription(), Value = x.ToString() }).ToList();
if (firstOption != null)
{
list.Insert(0, new ListItem() { Text = firstOption, Value = "" });
}
return list;
}
更新
很抱歉,我忘记放置GetDescption扩展名:
public static string GetDescription(this object obj)
{
var fi = obj.GetType().GetField(obj.ToString());
var attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
if (attributes.Length > 0)
return attributes[0].Description;
return obj.ToString();
}
您不需要创建CustomAttribute,只需使用@Peyman即可。您可以详细说明吗?我添加了答案,请检查,如果不清楚,请告诉我。不起作用。第一行必须更改为:
var items=Enum.GetValues(typeof(T)).Cast().ToList()
。但是我仍然在GetDescription()
@Ivan MarkDebono上遇到编译错误。对不起,我忘了放GetDescription扩展方法,请检查更新的代码