如何在C#中将int转换为枚举?


当前回答

简单地,可以将int强制转换为enum

 public enum DaysOfWeeks
    {
        Monday = 1,
        Tuesday = 2,
        Wednesday = 3,
        Thursday = 4,
        Friday = 5,
        Saturday = 6,
        Sunday = 7,
    } 

    var day= (DaysOfWeeks)5;
    Console.WriteLine("Day is : {0}", day);
    Console.ReadLine();

其他回答

向Enum转换和从Enum转换的不同方式

enum orientation : byte
{
 north = 1,
 south = 2,
 east = 3,
 west = 4
}

class Program
{
  static void Main(string[] args)
  {
    orientation myDirection = orientation.north;
    Console.WriteLine(“myDirection = {0}”, myDirection); //output myDirection =north
    Console.WriteLine((byte)myDirection); //output 1

    string strDir = Convert.ToString(myDirection);
        Console.WriteLine(strDir); //output north

    string myString = “north”; //to convert string to Enum
    myDirection = (orientation)Enum.Parse(typeof(orientation),myString);


 }
}

简单地,可以将int强制转换为enum

 public enum DaysOfWeeks
    {
        Monday = 1,
        Tuesday = 2,
        Wednesday = 3,
        Thursday = 4,
        Friday = 5,
        Saturday = 6,
        Sunday = 7,
    } 

    var day= (DaysOfWeeks)5;
    Console.WriteLine("Day is : {0}", day);
    Console.ReadLine();

要将字符串转换为ENUM或将int转换为ENOM常量,我们需要使用ENUM.Parse函数。这是一段youtube视频https://www.youtube.com/watch?v=4nhx4VwdRDk这实际上是用字符串演示的,这同样适用于int。

代码如下所示,其中“红色”是字符串,“MyColors”是具有颜色常数的颜色ENUM。

MyColors EnumColors = (MyColors)Enum.Parse(typeof(MyColors), "Red");

它可以帮助您将任何输入数据转换为用户所需的枚举。假设您有一个类似下面的枚举,默认为int。请在枚举的第一个添加默认值。当与输入值不匹配时,在helpers方法中使用。

public enum FriendType  
{
    Default,
    Audio,
    Video,
    Image
}

public static class EnumHelper<T>
{
    public static T ConvertToEnum(dynamic value)
    {
        var result = default(T);
        var tempType = 0;

        //see Note below
        if (value != null &&
            int.TryParse(value.ToString(), out  tempType) && 
            Enum.IsDefined(typeof(T), tempType))
        {
            result = (T)Enum.ToObject(typeof(T), tempType); 
        }
        return result;
    }
}

注意:这里我尝试将值解析为int,因为enum默认为int如果像这样定义枚举,则为字节类型。

public enum MediaType : byte
{
    Default,
    Audio,
    Video,
    Image
} 

您需要将helper方法的解析从

int.TryParse(value.ToString(), out  tempType)

to

byte.TryParse(value.ToString(),out tempType)

我检查我的方法是否有以下输入

EnumHelper<FriendType>.ConvertToEnum(null);
EnumHelper<FriendType>.ConvertToEnum("");
EnumHelper<FriendType>.ConvertToEnum("-1");
EnumHelper<FriendType>.ConvertToEnum("6");
EnumHelper<FriendType>.ConvertToEnum("");
EnumHelper<FriendType>.ConvertToEnum("2");
EnumHelper<FriendType>.ConvertToEnum(-1);
EnumHelper<FriendType>.ConvertToEnum(0);
EnumHelper<FriendType>.ConvertToEnum(1);
EnumHelper<FriendType>.ConvertToEnum(9);

对不起我的英语

从int:

YourEnum foo = (YourEnum)yourInt;

从字符串:

YourEnum foo = (YourEnum) Enum.Parse(typeof(YourEnum), yourString);

// The foo.ToString().Contains(",") check is necessary for 
// enumerations marked with a [Flags] attribute.
if (!Enum.IsDefined(typeof(YourEnum), foo) && !foo.ToString().Contains(","))
{
    throw new InvalidOperationException(
        $"{yourString} is not an underlying value of the YourEnum enumeration."
    );
}

从一个数字:

YourEnum foo = (YourEnum)Enum.ToObject(typeof(YourEnum), yourInt);