在遍历值的同时访问Enum自定义属性 [英] Access Enum custom attributes whilst looping over values
问题描述
我已经向一些枚举值添加了自定义属性(为它们提供了屏幕友好的字符串值)。我正在尝试建立要在MVC页面上使用的SelectListItems列表,但是访问自定义属性时遇到了麻烦。
I have added a custom attribute to some enum values (to give them a screen friendly string value). I am trying to build a list of SelectListItems for use on an MVC page, but I am running into trouble accessing the custom attribute.
我的枚举看起来像这样。
My enum looks like this.
public enum MyEnum
{
[StringValue("None")] None = 0,
[StringValue("First Value")] FirstValue = 1,
[StringValue("Second Value")] SecondValue = 2
}
属性看起来像这样。
public class StringValueAttribute : Attribute
{
public StringValueAttribute(string value)
{
this.StringValue = value;
}
public string StringValue { get; protected set; }
}
我创建了一个帮助器类,以便可以从中轻松访问StringValue属性
I created a helper class so that I can easily access the StringValue attribute from an instance of the Enum.
public static string GetStringValue(this Enum value)
{
Type type = value.GetType();
FieldInfo fieldInfo = type.GetField(value.ToString());
StringValueAttribute[] attribs = fieldInfo.GetCustomAttributes(typeof(StringValueAttribute), false) as StringValueAttribute[];
return attribs != null && attribs.Length > 0 ? attribs[0].StringValue : null;
}
我可以这样称呼。
MyEnum test = MyEnum.FirstValue;
string stringValue = test.GetStringValue();
最后,转到我所坚持的代码。我可以轻松地遍历Enum值,但是这些值不是MyEnum的实例,因此无法调用助手函数。当我尝试访问FieldInfo时,它总是返回null。
Finally, onto the code I am stuck with. I can loop over the Enum Values easily, but the values are not instances of MyEnum so I cannot call my helper function. And when I try to access the FieldInfo it always returns null. Here is what I have so far.
public static List<SelectListItem> GetFlagsSelectList<T>(int? selectedValue)
{
List<SelectListItem> items = new List<SelectListItem>();
foreach (int value in Enum.GetValues(typeof(T)))
{
items.Add(new SelectListItem
{
Text = Enum.GetName(typeof(T), value),
Value = value.ToString(),
Selected = selectedValue.HasValue && selectedValue.Value == value
});
}
return items;
}
是否可以在foreach循环中访问自定义属性?
Is it possible to access the custom attribute within the foreach loop?
编辑:
我想我对此不清楚。我想在foreach循环中访问custom属性。调用Enum.GetName(typeof(T),value)只是返回我不想要的属性的名称(例如FirstValue)。
I think I asked this unclearly. I would like to access the custom attribute inside the foreach loop. Calling Enum.GetName(typeof(T), value) simply returns the name of the property (e.g. FirstValue) which I do NOT want.
我想做点什么像这样:
foreach (int value in Enum.GetValues(typeof(T)))
{
string name = Enum.ToObject(typeof (T), value).GetStringValue();
}
但是T可以是任何类型,所以我不能调用GetStringValue()
But T could be any type so I can't call my GetStringValue() method there.
我尝试这样做:
foreach (int value in Enum.GetValues(typeof(T)))
{
FieldInfo fieldInfo = typeof(T).GetField(value.ToString());
StringValueAttribute[] attribs = fieldInfo.GetCustomAttributes(typeof(StringValueAttribute), false) as StringValueAttribute[];
string name = attribs != null && attribs.Length > 0 ? attribs[0].StringValue : Enum.GetName(typeof(T), value),;
items.Add(new SelectListItem
{
Text = name,
Value = value.ToString(),
Selected = selectedValue.HasValue && selectedValue.Value == value
});
}
但是我总是会遇到一个异常,因为FieldInfo对象总是返回null。
But I always get an exception because the FieldInfo object always returns null.
推荐答案
尝试
static string GetStringValue2(Enum value) {
....
}
public static List<SelectListItem> GetFlagsSelectList<T>(int? selectedValue) where T : struct {
var items = new List<SelectListItem>();
foreach (T value in Enum.GetValues(typeof(T))) {
var stringValue = GetStringValue2((Enum)(object)value);
items.Add(new SelectListItem {
Text = Enum.GetName(typeof(T), value),
Value = Convert.ToInt32(value).ToString(),
Selected = selectedValue.HasValue && selectedValue.Value == Convert.ToInt32(value)
});
}
return items;
}
这篇关于在遍历值的同时访问Enum自定义属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!