工作中 经常遇到枚举 的一些转换  特别是获取枚举备注等  特地整理下 方法以后使用

public void TestMethod1()
{
TestEnumOne colorEnum = TestEnumOne.Red;
int colorValue = 0x0000FF;
string colorStr = "Red";
string colorDes = "红色"; //枚举To枚举字符串
colorStr = colorEnum.ToString();
colorStr = Enum.GetName(typeof(TestEnumOne), colorEnum);
//枚举值To枚举字符串
colorStr = Enum.GetName(typeof(TestEnumOne), colorValue); //枚举To枚举值
colorValue = colorEnum.GetHashCode();
colorValue = (int)colorEnum;
//枚举字符To枚举值
colorValue = Enum.Parse(typeof(TestEnumOne), colorStr).GetHashCode();
colorValue = (int)Enum.Parse(typeof(TestEnumOne), colorStr); //枚举字To枚举
colorEnum = (TestEnumOne)Enum.Parse(typeof(TestEnumOne), colorStr);
//枚举值To枚举
colorEnum = (TestEnumOne)colorValue; //根据枚举获取备注
colorDes = TestEnumOne.Red.GetEnumDescriptionByEnum(typeof(TestEnumOne));
//根据枚举值获取备注
colorDes = TestEnumOne.Blue.GetEnumDescriptionByEnumValue(typeof(TestEnumOne), colorValue);
//根据枚举字符串获取备注
colorDes = TestEnumOne.Blue.GetEnumDescriptionByEnumString(typeof(TestEnumOne), colorStr);
}

  下面是一个 枚举  和上面用到的一些方法

public static class EnumClass
{
public enum TestEnumOne
{
[Description("红色")]
Red = 0xff0000,
[Description("橙色")]
Orange = 0xFFA500,
[Description("黄色")]
Yellow = 0xFFFF00,
[Description("蓝色")]
Blue = 0x0000FF,
} /// <summary>
/// 根据枚举获取备注
/// </summary>
/// <param name="aEnum">枚举</param>
/// <param name="enumType">枚举类型</param>
/// <returns>备注</returns>
public static string GetEnumDescriptionByEnum(this Enum aEnum, System.Type enumType)
{
string enumDescription = string.Empty;
foreach (System.Enum enumItem in System.Enum.GetValues(enumType))
{
string enumString = Enum.GetName(enumType, aEnum);
if (enumString.ToLower() == Enum.GetName(enumType, enumItem).ToLower())
{
FieldInfo fi = enumType.GetField(enumString);
DescriptionAttribute da = (DescriptionAttribute)Attribute.GetCustomAttribute(fi, enumType);
if (da != null)
{
enumDescription = da.Description;
}
}
}
return enumDescription;
} /// <summary>
/// 根据枚举值获取备注
/// </summary>
/// <param name="aEnum">枚举</param>
/// <param name="enumType">枚举类型</param>
/// <param name="enumValue">枚举值</param>
/// <returns>备注</returns>
public static string GetEnumDescriptionByEnumValue(this Enum aEnum, System.Type enumType, int enumValue)
{
string enumDescription = string.Empty;
foreach (System.Enum enumItem in System.Enum.GetValues(enumType))
{
if (enumItem.GetHashCode() == (int)enumValue)
{
string enumString = Enum.GetName(enumType, enumValue);
FieldInfo fi = enumType.GetField(enumString);
DescriptionAttribute da = (DescriptionAttribute)Attribute.GetCustomAttribute(fi, enumType);
if (da != null)
{
enumDescription = da.Description;
}
}
}
return enumDescription;
} /// <summary>
/// 根据枚举字符串获取备注
/// </summary>
/// <param name="aEnum">枚举</param>
/// <param name="enumType">枚举类型</param>
/// <param name="enumValue">枚举值</param>
/// <returns>备注</returns>
public static string GetEnumDescriptionByEnumString(this Enum aEnum, System.Type enumType, string enumString)
{
string enumDescription = string.Empty;
foreach (System.Enum enumItem in System.Enum.GetValues(enumType))
{
if (enumString.ToLower() == Enum.GetName(enumType, enumItem).ToLower())
{
FieldInfo fi = enumType.GetField(enumString);
DescriptionAttribute da = (DescriptionAttribute)Attribute.GetCustomAttribute(fi, enumType);
if (da != null)
{
enumDescription = da.Description;
}
}
}
return enumDescription;
}
}

C# 中 枚举Enum 一些转换的方法整理的更多相关文章

  1. 关于Java中枚举Enum的深入剖析

    在编程语言中我们,都会接触到枚举类型,通常我们进行有穷的列举来实现一些限定.Java也不例外.Java中的枚举类型为Enum,本文将对枚举进行一些比较深入的剖析. 什么是Enum Enum是自Java ...

  2. C++和Java中枚举enum的用法

    在C++和java中都有枚举enum这个关键字,但是它们之间又不太一样.对于C++来说,枚举是一系列命名了的整型常量,而且从枚举值转化为对应的整型值是在内部进行的.而对于Java来说,枚举更像一个类的 ...

  3. JAVA中枚举Enum详解

    1.关键字:enum.枚举可以定义成单独的文件,也可以定义在其他类内部. 枚举在类内部的示例: public class EnumInner { public static void main(Str ...

  4. Python中模拟enum枚举类型的5种方法分享

    这篇文章主要介绍了Python中模拟enum枚举类型的5种方法分享,本文直接给出实现代码,需要的朋友可以参考下   以下几种方法来模拟enum:(感觉方法一简单实用) 复制代码代码如下: # way1 ...

  5. MVC3不能正确识别JSON中的Enum枚举值

    一.背景 在MVC3项目里,如果Action的参数中有Enum枚举作为对象属性的话,使用POST方法提交过来的JSON数据中的枚举值却无法正确被识别对应的枚举值. 二.Demo演示 为了说明问题,我使 ...

  6. springboot mybatis自定义枚举enum转换

    原文链接:https://blog.csdn.net/u014527058/article/details/62883573 一.概述 在利用Spring进行Web后台开发时,经常会遇到枚举类型的绑定 ...

  7. [转载] Java中枚举类型的使用 - enum

    目录 1 枚举类的编译特性 2 向枚举类中添加方法 3 接口内部创建枚举 4 枚举类中使用枚举 5 扩展: 验证values()不是通过父类继承的 本文转载自博客 - Java枚举类型, 博主对原文内 ...

  8. Java中的enum枚举类

    首先说说为什么要写这个enum枚举类吧,是群里有个新手问:怎样把enum类中的值遍历得到,其实自己用的也很少.自己也是确实不知道,于是我去网上搜了不少,总结了些,希望对大家有帮助:首先我说说怎样遍历枚 ...

  9. Android中是否推荐使用枚举Enum

    一.Enum的产生 Java1.5中引入了枚举的语法,包括Enum,EnumSet,EnumMap等.其中Enum就是我们在C或C++中见过的枚举类型,但是Java中的枚举又比C或C++中的枚举更成熟 ...

随机推荐

  1. ASE课程总结 by 冯晓云

    开始的开始,采访往届ASE班的blog:http://www.cnblogs.com/legs/p/4894362.html 和北航软工M1检查:http://www.cnblogs.com/legs ...

  2. vue2.x学习笔记(十)

    接着前面的内容:https://www.cnblogs.com/yanggb/p/12584237.html. 事件处理 使用javascript当然少不了事件处理,即使是vue也不会例外. 监听事件 ...

  3. EF多租户实例:演变为读写分离

    前言 我又来写关于多租户的内容了,这个系列真够漫长的. 如无意外这篇随笔是最后一篇了.内容是讲关于如何利用我们的多租户库简单实现读写分离. 分析 对于读写分离,其实有很多种实现方式,但是总体可以分以下 ...

  4. python 工具链 包管理工具 pip

    Installation mac下可以采用 brew,easy_install(python自带)等方式安装. centos下可以采用yum,easy_install等方式安装. 但是上面两种方式在系 ...

  5. Go语言: 万物皆异步

    来源:https://www.jianshu.com/p/62c0cd107da3 同步和异步.阻塞和非阻塞 首先要明确的是,同步(Synchronous)和异步(Asynchronous),阻塞(B ...

  6. thinkphp--多个id查询

    $feedback_list = $feedback -> where( array("member_id"=>array("in", " ...

  7. 深入理解Java枚举

    深入理解Java枚举 重新认识Java枚举 老实说,挺羞愧的,这么久了,一直不知道Java枚举的本质是啥,虽然也在用,但是真不知道它的底层是个啥样的 直到2020年4月28日的晚上20点左右,我才真的 ...

  8. Inno Setup 删除文件夹 DelTree

    Pascal Scripting: DelTree Prototype: function DelTree(const Path: String; const IsDir, DeleteFiles, ...

  9. AtomineerUtils使用说明

    AtomineerUtils使用说明 VS2015PluginCrackAtomineer 介绍 AtomineerUtils 是国外的一款用于生成源代码注释的一款 VS 插件工具. 这款插件,支持 ...

  10. Scala教程之:可变和不变集合

    文章目录 mutable HashMap immutable HashMap 集合在程序中是非常有用的,只有用好集合才能真正感受到该语言的魅力.在scala中集合主要在三个包里面:scala.coll ...