获取Enum值的名称

时间:2022-02-13 23:40:46

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

3 个解决方案

#1


26  

Given an 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);
}

#2


8  

In C#, that would be:

在C#中,那将是:

return Enum.ToObject(typeof(T), EnumValue).ToString();

or (equally):

return ((T)(object)(EnumValue)).ToString();

#3


4  

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>();

这个字符串名称= someEnumValue.ToEnumName ();

are equivalent... but.. whatever suits you.

是等同的......但是......无论什么适合你。

#1


26  

Given an 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);
}

#2


8  

In C#, that would be:

在C#中,那将是:

return Enum.ToObject(typeof(T), EnumValue).ToString();

or (equally):

return ((T)(object)(EnumValue)).ToString();

#3


4  

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>();

这个字符串名称= someEnumValue.ToEnumName ();

are equivalent... but.. whatever suits you.

是等同的......但是......无论什么适合你。