You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
using System; using System.Collections.Generic;
namespace EasyBL.WebApi.Common { public static class EnumExtension { 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) { 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<string, string>(); 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; } } }
|