using System; using System.Collections.Generic; namespace EasyBL.WebApi.Common { public static class EnumExtension { private static Dictionary> enumCache; private static Dictionary> EnumCache { get { if (enumCache == null) { enumCache = new Dictionary>(); } return enumCache; } set { enumCache = value; } } public static string GetEnumText(this Enum en) { var enString = string.Empty; if (null == en) return enString; var type = en.GetType(); enString = en.ToString(); if (!EnumCache.ContainsKey(type.FullName)) { var fields = type.GetFields(); var temp = new Dictionary(); foreach (var item in fields) { var attrs = item.GetCustomAttributes(typeof(TextAttribute), false); if (attrs.Length == 1) { var v = ((TextAttribute)attrs[0]).Value; temp.Add(item.Name, v); } } EnumCache.Add(type.FullName, temp); } if (EnumCache[type.FullName].ContainsKey(enString)) { return EnumCache[type.FullName][enString]; } return enString; } } public sealed class TextAttribute : Attribute { public TextAttribute(string value) { Value = value; } public string Value { get; set; } } }