如何在c#中获取分配给my model return as List的所有枚举值?
我计划使用Enum填写我的国家/地区列表下拉列表。所以我需要枚举值描述和索引值。 我的条件:如何在c#中获取分配给my model return as List的所有枚举值?,c#,enums,C#,Enums,我计划使用Enum填写我的国家/地区列表下拉列表。所以我需要枚举值描述和索引值。 我的条件: public enum CountryListEnum { [Description("United Kingdom")] UnitedKingdom = 0, [Description("United States")] UnitedStates = 1, [Description("Afghanistan")]
public enum CountryListEnum
{
[Description("United Kingdom")]
UnitedKingdom = 0,
[Description("United States")]
UnitedStates = 1,
[Description("Afghanistan")]
Afghanistan = 2,
[Description("Albania")]
Albania = 3,
}
public class CountryModel
{
public int CountryId { get; set; }
public string CountryName { get; set; }
}
public enum CountryListEnum
{
[Description("United Kingdom")]
UnitedKingdom = 0,
[Description("United States")]
UnitedStates = 1,
[Description("Afghanistan")]
Afghanistan = 2,
[Description("Albania")]
Albania = 3,
}
public class CountryModel
{
public int CountryId { get; set; }
public string CountryName { get; set; }
}
我的型号:
public enum CountryListEnum
{
[Description("United Kingdom")]
UnitedKingdom = 0,
[Description("United States")]
UnitedStates = 1,
[Description("Afghanistan")]
Afghanistan = 2,
[Description("Albania")]
Albania = 3,
}
public class CountryModel
{
public int CountryId { get; set; }
public string CountryName { get; set; }
}
我认为这应该有助于您实现
foreach (var item in Enum.GetValues(typeof(CountryListEnum)))
{
CountryModel myModel = new CountryModel();
myModel.CountryId = item.GetHashCode();
myModel.CountryName = item.ToString();
}
编辑
正如其他人所指出的,上面的描述将无法检索。下面是如何实现描述属性重试的更新
foreach (var item in Enum.GetValues(typeof(CountryListEnum)))
{
CountryModel myModel = new CountryModel();
myModel.CountryId = item.GetHashCode();
var type = typeof(CountryListEnum);
var memInfo = type.GetMember(item.ToString());
var attributes = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
var description = ((DescriptionAttribute)attributes[0]).Description;
myModel.CountryName = description;
}
要获取索引值,您只需将枚举强制转换为int。获取description属性稍微复杂一些。也许是这样的
public enum CountryListEnum
{
[Description("United Kingdom")]
UnitedKingdom = 0,
[Description("United States")]
UnitedStates = 1,
[Description("Afghanistan")]
Afghanistan = 2,
[Description("Albania")]
Albania = 3,
}
static void Main(string[] args)
{
foreach (var country in Enum.GetValues(typeof(CountryListEnum)).Cast<CountryListEnum>())
{
Console.WriteLine($"Index: {(int)country}");
Console.WriteLine($"Description: {GetDescription(country)}");
}
}
public static string GetDescription(Enum value)
{
Type type = value.GetType();
string name = Enum.GetName(type, value);
if (name != null)
{
System.Reflection.FieldInfo field = type.GetField(name);
if (field != null)
{
if (Attribute.GetCustomAttribute(field,
typeof(DescriptionAttribute)) is DescriptionAttribute attr)
{
return attr.Description;
}
}
}
return null;
}
public enum CountryListEnum
{
[说明(“联合王国”)]
英国=0,
[说明(“美国”)]
美国=1,
[说明(“阿富汗”)]
阿富汗=2,
[说明(“阿尔巴尼亚”)]
阿尔巴尼亚=3,
}
静态void Main(字符串[]参数)
{
foreach(Enum.GetValues中的var country(typeof(CountryListEnum)).Cast())
{
Console.WriteLine($“索引:{(int)country}”);
Console.WriteLine($“Description:{GetDescription(country)}”);
}
}
公共静态字符串GetDescription(枚举值)
{
Type Type=value.GetType();
字符串名称=Enum.GetName(类型、值);
if(name!=null)
{
System.Reflection.FieldInfo字段=type.GetField(名称);
如果(字段!=null)
{
if(属性)。GetCustomAttribute(字段,
typeof(DescriptionAttribute))是DescriptionAttribute属性(属性)
{
返回属性描述;
}
}
}
返回null;
}
我想,这就是你要找的
var model = new List<CountryModel>();
foreach (var item in Enum.GetValues(typeof(CountryListEnum)))
{
model.Add(new CountryModel
{
CountryId = (int)item,
CountryName = ((DescriptionAttribute)item.GetType().GetField(item.ToString()).GetCustomAttribute(typeof(DescriptionAttribute), false)).Description
});
}
var模型=新列表();
foreach(Enum.GetValues中的变量项(typeof(CountryListEnum)))
{
model.Add(新的CountryModel)
{
CountryId=(int)项,
CountryName=((DescriptionAttribute)item.GetType().GetField(item.ToString()).GetCustomAttribute(typeof(DescriptionAttribute),false)).Description
});
}
什么阻止了你?什么是枚举索引?@lzzy我不知道该怎么办。我期待通用的单行代码,如generic@TheGeneral那是0,1,2。。该值我需要将索引值设置为国家id,将描述值设置为国家名称。这会将英国国家名称设置为“联合王国”(不带空格),而不是“联合王国”。也许这已经足够好了,但是为什么你会有描述呢?他在寻找“描述”这个项目,检查我的回答我无法编译这个。它抱怨GetCustomAttribute的参数应该是bool,而不是类型。@HansKilian我错了,现在您可以复制它了。