这是一个"怪异"的问题:
是否有可能创建一种方法,在其中将任何枚举转换为列表.这是我目前正在思考的草案.
public class EnumTypes
{
public enum Enum1
{
Enum1_Choice1 = 1,
Enum1_Choice2 = 2
}
public enum Enum2
{
Enum2_Choice1 = 1,
Enum2_Choice2 = 2
}
public List<string> ExportEnumToList(<enum choice> enumName)
{
List<string> enumList = new List<string>();
//TODO: Do something here which I don't know how to do it.
return enumList;
}
}
Run Code Online (Sandbox Code Playgroud)
只是好奇是否可能以及如何做到这一点.
Moh*_*oho 11
Enum.GetNames( typeof(EnumType) ).ToList()
Run Code Online (Sandbox Code Playgroud)
http://msdn.microsoft.com/en-us/library/system.enum.getnames.aspx
或者,如果你想得到幻想:
public static List<string> GetEnumList<T>()
{
// validate that T is in fact an enum
if (!typeof(T).IsEnum)
{
throw new InvalidOperationException();
}
return Enum.GetNames(typeof(T)).ToList();
}
// usage:
var list = GetEnumList<EnumType>();
Run Code Online (Sandbox Code Playgroud)