获取列表< string>我的枚举属性使用通用方法
开始时,我们有这个基本的枚举。
At start, we have this basic enum.
public enum E_Levels {
[ValueOfEnum("Low level")]
LOW,
[ValueOfEnum("Normal level")]
NORMAL,
[ValueOfEnum("High level")]
HIGH
}
我想得到一个列表< string> 无论枚举。像 Extensions.GetValuesOfEnum< E_Levels>()
可能会返回一个列表< string>
与低级 正常级别和高级别中。
And I would like to get a List<string>
whatever the enum. Something like Extensions.GetValuesOfEnum<E_Levels>()
which could return a List<string>
with "Low level", "Normal level" and "High level" in it.
StackOF帮助我获取一个值属性:
StackOF helped me to get one value attribute :
public static class Extensions {
public static string ToValueOfEnum(this Enum value) {
FieldInfo fieldInfo = value.GetType().GetField(value.ToString());
ValueOfEnum[] attribs = fieldInfo.GetCustomAttributes(typeof(ValueOfEnum), false) as ValueOfEnum[];
return attribs.Length > 0 ? attribs[0].value : null;
}
}
我可以调用此方法无论枚举: code> E_Levels.LOW.ToValueOfEnum()。
And I can call this method whatever the enum : E_Levels.LOW.ToValueOfEnum()
.
此外,StackOF帮助我获得一个列表< ; 特定枚举的字符串>
我在控制器中做了这个方法:
Furthermore, StackOF helped me to get a List<string>
for a specific enum.
I made this method in a controller :
private List<string> GetLevels() {
List<string> levelsToReturn = new List<string>();
var levels = Enum.GetValues(typeof(E_Levels)).Cast<E_Levels>();
foreach(E_Levels l in levels)
levelsToReturn.Add(l.ToValueOfEnum());
return levelsToReturn;
}
但是,这样需要我为每个枚举重写相同的方法。 />
所以我试图添加这个泛型方法我的类Extensions:
But this way requires me to rewrite the same method for each enum.
So I tried to add this generic method my class Extensions :
public static class Extensions {
public static string ToValueOfEnum(this Enum value) {...}
public static List<string> GetValuesOf<T>() {
List<string> levelsToReturn = new List<string>();
var levels = Enum.GetValues(typeof(T)).Cast<T>();
foreach(T l in levels)
levelsToReturn.Add(l.ToValueOfEnum());
return levelsToReturn;
}
}
但是在我的foreach中, .ToValueOfEnum()
是一个未知的方法。
所以我很麻烦,我希望我可以找到一种方法,每次都不会重复使用相同的方法枚举...
But in my foreach, .ToValueOfEnum()
is an unknown method.
So I am in trouble, I hoped I could find a way to not rewrite again and again the same method for each enum...
让我们尽量保持这个更通用的目的。
Let's try to keep this more general purpose.
我有一个扩展方法,可以从枚举值中获取属性。这将让您快速访问属性。
I have an extension method that could grab attributes off of enum values. This would give you quick access to the attributes.
public static class EnumExtensions
{
public static TAttribute GetAttribute<TAttribute>(this Enum value)
where TAttribute : Attribute
{
var type = value.GetType();
var name = Enum.GetName(type, value);
return type.GetField(name)
.GetCustomAttributes(false)
.OfType<TAttribute>()
.SingleOrDefault();
}
}
使用这个,你可以创建一些查询来获取什么你想要。
Using this, you could create some queries to get what you want.
var valuesOfLevels =
Enum.GetValues(typeof(E_Levels)).Cast<E_Levels>()
.Select(level => level.GetAttribute<ValueOfEnumAttribute>().Value);
所以你的 GetValuesOf()
方法不是一个这样的专业方法的伟大的名字IMHO)可以这样写:
So your GetValuesOf()
method (which is not a great name for such a specialty method IMHO) can be written like this:
public static List<string> GetValuesOf<TEnum>()
where TEnum : struct // can't constrain to enums so closest thing
{
return Enum.GetValues(typeof(TEnum)).Cast<Enum>()
.Select(val => val.GetAttribute<ValueOfEnumAttribute>().Value)
.ToList();
}
现在你可以这样调用方法:
Now you may call the method like so:
var levelValues = GetValueOf<E_Levels>();
// levelValues = { "Low level", "Normal level", "High level" }