我有枚举值的 StringValue
属性,所以我可以为每个值附加一个描述:
public class StringValueAttribute : Attribute
{
public string Value { get; private set; }
public StringValueAttribute(string value)
{
Value = value;
}
}
这就是我使用它的方式:
enum Group
{
[StringValue("Computer Science")]
ComputerScience,
[StringValue("Software Engineering")]
SoftwareEngineering,
// ... additional values follow.
}
我有一个方法,在给定枚举值的情况下检索 StringValue
:
public static string GetStringValue(Enum value)
{
Type type = value.GetType();
FieldInfo fieldInfo = type.GetField(type.ToString());
StringValueAttribute[] attributes = fieldInfo.GetCustomAttributes(typeof(StringValueAttribute), false) as StringValueAttribute[];
string stringValue = null;
if (attributes.Length > 0)
{
stringValue = attributes[0].Value;
}
return stringValue;
}
我想要另一个获取枚举的方法(枚举本身,而不是值)并使用 GetStringValue
方法检索 IEnumerable
. 我不知道如何做到这一点 . 这样的方法怎么样?
Edit: 此问题与How to get C# Enum description from value?不重复 . 我知道如何获取枚举属性值,我实际上在问题中有一个方法就是这样做的 . 我的问题是如何枚举枚举中的所有属性 .
2 回答
最直接的方法是使用泛型,尽管您可以随时传入特定
Enum
的实例,获取其类型,然后为其所有值返回StringValue
值:笔记:
c#中没有
where TEnum : Enum
约束 . 将TEnum
限制为struct, IConvertible, IComparable, IFormattable
基本上就足够了 .即便如此,有一个狡猾的技巧来应用
enum
约束,该约束在this answer中由Enum type constraints in C#显示为Enum type constraints in C# . (我真的很狡猾 . )如@ EdPlunkett的评论中所述,您需要将
value.ToString()
传递给type.GetField()
,因为您获得的字段对应于该特定的传入enum
值 .样品fiddle
这应该工作: