.net 获取枚举值的名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16039037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get the name of Enum value
提问by ElektroStudios
I'm trying to make a function where we can get the Namevalue of a EnumValue
我正在尝试创建一个函数,我们可以在其中获取 EnumValue 的 Namevalue
For example:
例如:
Get_Enum_ValueName(DayOfWeek, 0)
...This will return "Sunday".
...这将返回“星期日”。
But my code don't works, it says the type is not defined:
但是我的代码不起作用,它说未定义类型:
Private Function Get_Enum_ValueName(Of T)(ByVal EnumName As T, ByVal EnumValue As Integer) As String
Return DirectCast([Enum].Parse(GetType(EnumName), EnumValue ), EnumName).ToString
End Function
回答by ja72
Given an enum
给定一个 enum
public enum Week
{
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
}
here are the things you can do:
您可以执行以下操作:
static void Main(string[] args)
{
// enum to int
int i=(int)Week.Thursday;
// int to enum;
Week day=(Week)3;
// enum to string
string name=Week.Thursday.ToString();
string fun=Enum.GetName(typeof(Week), 6);
string agh=Enum.GetName(typeof(Week), Week.Monday);
string wed=EnumName(Week.Wednesday);
// string to enum
Week apt=(Week)Enum.Parse(typeof(Week), "Thursday");
// all values of an enum type
Week[] days=(Week[])Enum.GetValues(typeof(Week));
// all names of an enum type
string[] names=Enum.GetNames(typeof(Week));
}
static string EnumName<T>(T value)
{
return Enum.GetName(typeof(T), value);
}
Edit 1
编辑 1
If you want to convert from one enumto another enumof different type based on the underlying numeric value (convert to integer and from integer), then use the following:
如果要根据基础数值从一种类型转换enum为另一种enum不同类型(转换为整数和整数),请使用以下命令:
/// <summary>
/// Casts one enum type to another based on the underlying value
/// </summary>
/// <typeparam name="TEnum">The type of the enum.</typeparam>
/// <param name="otherEnum">The other enum.</param>
public static TEnum CastTo<TEnum>(this Enum otherEnum)
{
return (TEnum)Enum.ToObject(typeof(TEnum), Convert.ToInt32(otherEnum));
}
to be used as
用作
public enum WeekEnd
{
Saturday = Week.Saturday,
Sunday = Week.Sunday
}
static void Main(string[] args)
{
var day = WeekEnd.Saturday.CastTo<Week>();
// Week.Sunday
}
回答by Marc Gravell
In C#, that would be:
在 C# 中,这将是:
return Enum.ToObject(typeof(T), EnumValue).ToString();
or (equally):
或(同样):
return ((T)(object)(EnumValue)).ToString();
回答by Matt
public enum WeekDay
{
Monday = 1,
Tuesday = 2,
Wednesday = 3,
Thursday = 4,
Friday = 5,
Saturday = 6,
Sunday = 7
}
string s = WeekDay.Friday.ToString();
simple as that... unless I am misunderstanding something?
就这么简单……除非我误解了什么?
And if you only have the number:
如果你只有号码:
string s = ((WeekDay)4).ToString();
UPDATE
更新
OK, next time you should mention that you want something generic.. to use for all enums and not just that specific example. You can try this:
好的,下次你应该提到你想要一些通用的东西......用于所有枚举,而不仅仅是那个特定的例子。你可以试试这个:
public static class EnumExtensions
{
public static T ToEnum<T>(this int value) where T : struct
{
return (T)(object)value;
}
public static string ToEnumName<T>(this int value) where T : struct
{
return ((T)(object)value).ToString();
}
}
Use like this:
像这样使用:
int someEnumValue = 4;
string name = someEnumValue.ToEnumName<WeekDay>();
or:
或者:
WeekDay weekDay = someEnumValue.ToEnum<WeekDay>();
I still don't think that is really necessary though, because you still need to know the type of enum anyway... so therefore:
我仍然不认为这真的有必要,因为无论如何您仍然需要知道枚举的类型......因此:
This: string name = ((WeekDay)someEnumValue).ToString();
这个: string name = ((WeekDay)someEnumValue).ToString();
and this string name = someEnumValue.ToEnumName<WeekDay>();
和这个 string name = someEnumValue.ToEnumName<WeekDay>();
are equivalent... but.. whatever suits you.
是等价的......但是......任何适合你的。

