zoukankan      html  css  js  c++  java
  • 枚举处理

    //=======================================================================================
    /****************************************************************************************
    *
    * 文件说明:
    * 作者:
    * 创始时间:2017/11/9 17:23:01
    * 创建说明:
    *****************************************************************************************/
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace Manjinba.Communication.Common.Utils
    {
    public static class EnumUtil
    {
    private static Dictionary<string, Dictionary<string, string>> enumCache;

    private static Dictionary<string, Dictionary<string, string>> EnumCache
    {
    get
    {
    if (enumCache == null)
    {
    enumCache = new Dictionary<string, Dictionary<string, string>>();
    }
    return enumCache;
    }
    set { enumCache = value; }
    }

    public static string GetEnumText(this Enum en)
    {
    string enString = string.Empty;
    if (null == en) return enString;
    var type = en.GetType();
    enString = en.ToString();
    if (!EnumCache.ContainsKey(type.FullName))
    {
    var fields = type.GetFields();
    Dictionary<string, string> temp = new Dictionary<string, string>();
    foreach (var item in fields)
    {
    var attrs = item.GetCustomAttributes(typeof(DescriptionAttribute), false);
    if (attrs.Length == 1)
    {
    string v = ((DescriptionAttribute)attrs[0]).Description;
    temp.Add(item.Name, v);
    }
    }
    EnumCache.Add(type.FullName, temp);
    }
    if (EnumCache.ContainsKey(type.FullName))
    {
    if (EnumCache[type.FullName].ContainsKey(enString))
    {
    return EnumCache[type.FullName][enString];
    }
    }
    return enString;
    }
    }
    }

  • 相关阅读:
    02-链路层
    01-TCP/IP概述
    ARM Cortex-A9 (tiny 4412)
    STM32 f407 温湿度采集报警
    arduino mega 避障报距小车
    归纳法调试
    python 数据类型Ⅲ(字典)
    Python 数据类型Ⅱ(列表,元祖)
    Python 数据类型(str,int,bool)
    Python while循环&格式化输出&运算符
  • 原文地址:https://www.cnblogs.com/Nine4Cool/p/10540655.html
Copyright © 2011-2022 走看看