C# - Enum各种转换

复制代码
[Flags]
public enum EMyType
{
    None = 0,
    One = 1<<0,
    Tow = 1<<1,
    There = 1<<2
}

public EMyType MyType;

1.Enum转int

复制代码
 var intValue = (int)MyType
复制代码
var enumIntValue = 1;
var enumValue = (Enum)Enum.ToObject(typeof(EMyType),enumIntValue)
enumValue = EditorGUILayout.EnumFlagsField(enumValue);
var intValue = System.Convert.ToInt64(enumValue );

//或者
var intValue = (int)((object)enumValue );

2. int转Enum

复制代码
var intValue = 1;
EMyType enumValue = (EMyType)intValue;
复制代码
var intValue = 1;
Enum enumValue = (Enum)Enum.ToObject(typeof(EMyType), intValue);
相关推荐
宝桥南山12 小时前
GitHub Models - 尝试一下使用GitHub Models
microsoft·ai·微软·c#·github·.netcore
hixiong12315 小时前
C# OpenvinoSharp部署INSID3
开发语言·人工智能·ai·c#·openvinosharp
星辰徐哥16 小时前
Unity C#入门:变量的定义与访问权限(public/private)
unity·c#·lucene
leoufung16 小时前
LeetCode 30:Substring with Concatenation of All Words 题解(含 C 语言 uthash 实现)
c语言·leetcode·c#
hacker70716 小时前
Visual Studio安装教程(C#开发版)
ide·c#·visual studio
SKY -dada16 小时前
Understand 使用教程
开发语言·c#·流程图·软件构建·敏捷流程·代码复审·源代码管理
William_cl19 小时前
【C#/.NET 进阶】ASP.NET 架构与最佳实践:DI 依赖注入(IoC 核心)从入门到避坑
c#·asp.net·.net
武藤一雄19 小时前
WPF:MessageBox系统消息框
前端·microsoft·c#·.net·wpf
武藤一雄19 小时前
WPF进阶:万字详解WPF如何性能优化
windows·性能优化·c#·.net·wpf·.netcore·鲁棒性
xiaogutou11211 天前
2026年历史课件PPT模板选购指南:教师备课效率与精度的平衡方案
开发语言·c#