如何将int数字类型转换为枚举?


如何在C#中将int强制转换enum

从字符串:

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

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

从int:

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

相关阅读 >>

unsigned int几个字节

enum扩展特性实例代码

如何将int数字类型转换为枚举?

【c#教程】c# 枚举(enum)

c#如何枚举枚举

在c,c ++和c#中的int是什么

c#中enum与string的相互转换的示例

int[]转换为byte[]

更多相关阅读请进入《int》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...