枚举处理

//=======================================================================================
/****************************************************************************************
*
* 文件说明:
* 作者:
* 创始时间: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;
}
}
}

posted @ 2019-03-16 09:10  Nine4酷  阅读(97)  评论(0编辑  收藏  举报