如何从值中获取 C# Enum 描述?
我有一个带有如下描述属性的枚举:
public enum MyEnum
{
Name1 = 1,
[Description("Here is another")]
HereIsAnother = 2,
[Description("Last one")]
LastOne = 3
}
我发现了这段用于基于枚举检索描述的代码
public static string GetEnumDescription(Enum value)
{
FieldInfo fi = value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes = fi.GetCustomAttributes(typeof(DescriptionAttribute), false) as DescriptionAttribute[];
if (attributes != null && attributes.Any())
{
return attributes.First().Description;
}
return value.ToString();
}
这允许我编写如下代码:
var myEnumDescriptions = from MyEnum n in Enum.GetValues(typeof(MyEnum))
select new { ID = (int)n, Name = Enumerations.GetEnumDescription(n) };
我想要做的是如果我知道枚举值(例如 1) - 如何我可以检索描述吗?换句话说,如何将整数转换为“枚举值”以传递给我的 GetDescription 方法?
I have an enum with Description attributes like this:
public enum MyEnum
{
Name1 = 1,
[Description("Here is another")]
HereIsAnother = 2,
[Description("Last one")]
LastOne = 3
}
I found this bit of code for retrieving the description based on an Enum
public static string GetEnumDescription(Enum value)
{
FieldInfo fi = value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes = fi.GetCustomAttributes(typeof(DescriptionAttribute), false) as DescriptionAttribute[];
if (attributes != null && attributes.Any())
{
return attributes.First().Description;
}
return value.ToString();
}
This allows me to write code like:
var myEnumDescriptions = from MyEnum n in Enum.GetValues(typeof(MyEnum))
select new { ID = (int)n, Name = Enumerations.GetEnumDescription(n) };
What I want to do is if I know the enum value (e.g. 1) - how can I retrieve the description? In other words, how can I convert an integer into an "Enum value" to pass to my GetDescription method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
C# 中
enum
的默认基础数据类型是int
,您可以直接转换它。The default underlying data type for an
enum
in C# is anint
, you can just cast it.更新
Unconstrained Melody 库不再维护;放弃支持,转而支持 Enums.NET。
在 Enums.NET 中,您可以使用:
原始帖子
我以通用的、类型安全的方式在 Unconstrained Melody 中实现了这一点 - 你会使用:
这:
我意识到核心答案只是从
int
到MyEnum
的转换,但如果你做了很多枚举工作值得考虑使用 Unconstrained Melody :)Update
The Unconstrained Melody library is no longer maintained; Support was dropped in favour of Enums.NET.
In Enums.NET you'd use:
Original post
I implemented this in a generic, type-safe way in Unconstrained Melody - you'd use:
This:
I realise the core answer was just the cast from an
int
toMyEnum
, but if you're doing a lot of enum work it's worth thinking about using Unconstrained Melody :)我将代码从通用扩展方法中接受的答案中组合在一起,因此它可以用于所有类型的对象:
使用原始帖子中的枚举,或任何其他其属性用 Description 属性装饰的类,代码可以这样消费:
I put the code together from the accepted answer in a generic extension method, so it could be used for all kinds of objects:
Using an enum like in the original post, or any other class whose property is decorated with the Description attribute, the code can be consumed like this:
为了使其更易于使用,我编写了一个通用扩展:
现在我可以编写:
注意:将上面的“枚举”替换为您的类名
To make this easier to use, I wrote a generic extension:
now I can write:
Note: replace "Enumerations" above with your class name
您无法轻松地以通用方式执行此操作:您只能将整数转换为特定类型的枚举。正如 Nicholas 所示,如果您只关心一种枚举,那么这是一个微不足道的转换,但如果您想编写一个可以处理不同类型枚举的泛型方法,事情就会变得有点复杂。您需要一个类似于以下内容的方法:
但这会导致编译器错误“int 无法转换为 TEnum”(如果您解决此问题,则“TEnum 无法转换为 Enum”)。因此,您需要通过向对象插入强制转换来欺骗编译器:
您现在可以调用它来获取手头任何类型的枚举的描述:
You can't easily do this in a generic way: you can only convert an integer to a specific type of enum. As Nicholas has shown, this is a trivial cast if you only care about one kind of enum, but if you want to write a generic method that can handle different kinds of enums, things get a bit more complicated. You want a method along the lines of:
but this results in a compiler error that "int can't be converted to TEnum" (and if you work around this, that "TEnum can't be converted to Enum"). So you need to fool the compiler by inserting casts to object:
You can now call this to get a description for whatever type of enum is at hand: