如何在C#中将int转换为枚举?
当前回答
你应该建立一些类型匹配的放松机制,以更加健壮。
public static T ToEnum<T>(dynamic value)
{
if (value == null)
{
// default value of an enum is the object that corresponds to
// the default value of its underlying type
// https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/default-values-table
value = Activator.CreateInstance(Enum.GetUnderlyingType(typeof(T)));
}
else if (value is string name)
{
return (T)Enum.Parse(typeof(T), name);
}
return (T)Enum.ToObject(typeof(T),
Convert.ChangeType(value, Enum.GetUnderlyingType(typeof(T))));
}
测试用例
[Flags]
public enum A : uint
{
None = 0,
X = 1 < 0,
Y = 1 < 1
}
static void Main(string[] args)
{
var value = EnumHelper.ToEnum<A>(7m);
var x = value.HasFlag(A.X); // true
var y = value.HasFlag(A.Y); // true
var value2 = EnumHelper.ToEnum<A>("X");
var value3 = EnumHelper.ToEnum<A>(null);
Console.ReadKey();
}
其他回答
我正在使用这段代码将int转换为枚举:
if (typeof(YourEnum).IsEnumDefined(valueToCast)) return (YourEnum)valueToCast;
else { //handle it here, if its not defined }
我觉得这是最好的解决方案。
我认为要得到一个完整的答案,人们必须知道枚举在.NET内部是如何工作的。
工作原理
.NET中的枚举是一种将一组值(字段)映射到基本类型(默认值为int)的结构。但是,实际上可以选择枚举映射到的整数类型:
public enum Foo : short
在这种情况下,枚举被映射到短数据类型,这意味着它将作为短数据存储在内存中,并且在转换和使用它时表现为短数据。
如果从IL的角度来看,(normal,int)枚举如下所示:
.class public auto ansi serializable sealed BarFlag extends System.Enum
{
.custom instance void System.FlagsAttribute::.ctor()
.custom instance void ComVisibleAttribute::.ctor(bool) = { bool(true) }
.field public static literal valuetype BarFlag AllFlags = int32(0x3fff)
.field public static literal valuetype BarFlag Foo1 = int32(1)
.field public static literal valuetype BarFlag Foo2 = int32(0x2000)
// and so on for all flags or enum values
.field public specialname rtspecialname int32 value__
}
这里需要注意的是,value__与枚举值分开存储。在上述枚举Foo的情况下,value__的类型为int16。这基本上意味着,只要类型匹配,就可以在枚举中存储所需的任何内容。
在这一点上,我想指出System.Enum是一种值类型,这基本上意味着BarFlag将占用4个字节的内存,Foo将占用2个字节——例如,基础类型的大小(实际上比这更复杂,但嘿……)。
答案
因此,如果您有一个要映射到枚举的整数,则运行时只需执行两件事:复制4个字节并将其命名为其他名称(枚举的名称)。复制是隐式的,因为数据存储为值类型-这基本上意味着如果使用非托管代码,则可以简单地交换枚举和整数而不复制数据。
为了使其安全,我认为最好的做法是知道底层类型是相同的或隐式可转换的,并确保枚举值存在(默认情况下不检查它们!)。
要了解这是如何工作的,请尝试以下代码:
public enum MyEnum : int
{
Foo = 1,
Bar = 2,
Mek = 5
}
static void Main(string[] args)
{
var e1 = (MyEnum)5;
var e2 = (MyEnum)6;
Console.WriteLine("{0} {1}", e1, e2);
Console.ReadLine();
}
请注意,转换到e2也有效!从上面的编译器角度来看,这是有意义的:value__字段只填充了5或6,当Console.WriteLine调用ToString()时,e1的名称被解析,而e2的名称不被解析。
如果这不是您想要的,请使用Enum.IsDefined(typeof(MyEnum),6)检查您要转换的值是否映射到已定义的枚举。
还要注意,我对枚举的底层类型很明确,尽管编译器实际上检查了这一点。我这样做是为了确保我不会在路上遇到任何意外。要看到这些惊喜,您可以使用以下代码(实际上,我在数据库代码中见过很多这种情况):
public enum MyEnum : short
{
Mek = 5
}
static void Main(string[] args)
{
var e1 = (MyEnum)32769; // will not compile, out of bounds for a short
object o = 5;
var e2 = (MyEnum)o; // will throw at runtime, because o is of type int
Console.WriteLine("{0} {1}", e1, e2);
Console.ReadLine();
}
以以下示例为例:
int one = 1;
MyEnum e = (MyEnum)one;
我不知道从哪里得到这个枚举扩展的一部分,但它来自stackoverflow。我很抱歉!但我拿了这个,用Flags修改了enums。对于带有标志的枚举,我这样做了:
public static class Enum<T> where T : struct
{
private static readonly IEnumerable<T> All = Enum.GetValues(typeof (T)).Cast<T>();
private static readonly Dictionary<int, T> Values = All.ToDictionary(k => Convert.ToInt32(k));
public static T? CastOrNull(int value)
{
T foundValue;
if (Values.TryGetValue(value, out foundValue))
{
return foundValue;
}
// For enums with Flags-Attribut.
try
{
bool isFlag = typeof(T).GetCustomAttributes(typeof(FlagsAttribute), false).Length > 0;
if (isFlag)
{
int existingIntValue = 0;
foreach (T t in Enum.GetValues(typeof(T)))
{
if ((value & Convert.ToInt32(t)) > 0)
{
existingIntValue |= Convert.ToInt32(t);
}
}
if (existingIntValue == 0)
{
return null;
}
return (T)(Enum.Parse(typeof(T), existingIntValue.ToString(), true));
}
}
catch (Exception)
{
return null;
}
return null;
}
}
例子:
[Flags]
public enum PetType
{
None = 0, Dog = 1, Cat = 2, Fish = 4, Bird = 8, Reptile = 16, Other = 32
};
integer values
1=Dog;
13= Dog | Fish | Bird;
96= Other;
128= Null;
您只需执行以下操作:
int intToCast = 1;
TargetEnum f = (TargetEnum) intToCast ;
为了确保只计算正确的值否则可以抛出异常:
int intToCast = 1;
if (Enum.IsDefined(typeof(TargetEnum), intToCast ))
{
TargetEnum target = (TargetEnum)intToCast ;
}
else
{
// Throw your exception.
}
请注意,使用IsDefined的成本很高,甚至不仅仅是强制转换,因此决定是否使用它取决于您的实现。
推荐文章
- 我如何使一个方法的返回类型泛型?
- 何时处理CancellationTokenSource?
- 如何获取正在执行的程序集版本?
- AutoMapper vs valueinjector
- 为什么控制台不。Writeline,控制台。在Visual Studio Express中编写工作?
- 什么是.NET程序集?
- 字符串不能识别为有效的日期时间“格式dd/MM/yyyy”
- 函数应该返回空对象还是空对象?
- 如何转换日期时间?将日期时间
- 如何在c#中连接列表?
- 在c#中引用类型变量的“ref”的用途是什么?
- 将varchar字段的类型更改为整数:"不能自动转换为整数类型"
- 防止在ASP中缓存。NET MVC中使用属性的特定操作
- 将整数转换为字符串,以逗号表示千
- 转换为值类型'Int32'失败,因为物化值为空