/***************************************************************** * Module: EnumDescConverter.cs * Type: C# Source Code * Version: 1.0 * Description: Enum Converter using Description Attributes * * Revisions * ------------------------------------------------ * [F] 24/02/2004, Jcl - Shaping up * [B] 25/02/2004, Jcl - Made it much easier :-) * *****************************************************************/ using System; using System.Collections.Generic; using System.ComponentModel; using System.Reflection; namespace Krs.Ats.IBNet { /// /// EnumConverter supporting System.ComponentModel.DescriptionAttribute /// public static class EnumDescConverter { #region Private Caches /// /// Cache of enumerables /// private static Dictionary enumLookup = new Dictionary(); /// /// Enum Tuple /// private class EnumTuple { /// /// Enum Type /// public Type EnumType; /// /// Enumeration to String /// public Dictionary EnumToString = new Dictionary(); /// /// String to Enumeration /// public Dictionary DescriptionToEnum = new Dictionary(); /// /// Name to enum /// public Dictionary NameToEnum = new Dictionary(); } #endregion /// /// Constructor to create caches /// static EnumDescConverter() { Type[] types = Assembly.GetExecutingAssembly().GetTypes(); List enums = new List(); foreach(var type in types) { if (type.IsEnum) { //Add to cache enums.Add(type); } } foreach (var e in enums) { EnumTuple et = new EnumTuple(); et.EnumType = e; FieldInfo[] fields = e.GetFields(); foreach (var fi in fields) { if (fi.FieldType == e) { DescriptionAttribute[] attributes = (DescriptionAttribute[]) fi.GetCustomAttributes(typeof (DescriptionAttribute), false); Enum val = (Enum) fi.GetValue(fi.Name); string strVal = attributes.Length > 0 ? attributes[0].Description : val.ToString(); et.EnumToString.Add(val, strVal); if(attributes.Length > 0) et.DescriptionToEnum.Add(attributes[0].Description, val); et.NameToEnum.Add(val.ToString(), val); } } enumLookup.Add(e, et); } } /// /// Gets Enum Value's Description Attribute /// /// The value you want the description attribute for /// The description, if any, else it's .ToString() public static string GetEnumDescription(Enum value) { if (value == null) throw new ArgumentNullException("value"); Type t = value.GetType(); if (enumLookup.ContainsKey(t)) { EnumTuple et = enumLookup[t]; if(et.EnumToString.ContainsKey(value)) return et.EnumToString[value]; } FieldInfo fi = t.GetField(value.ToString()); DescriptionAttribute[] attributes = (DescriptionAttribute[]) fi.GetCustomAttributes( typeof (DescriptionAttribute), false); return (attributes.Length > 0) ? attributes[0].Description : value.ToString(); } /// /// Gets the value of an Enum, based on it's Description Attribute or named value /// /// The Enum type /// The description or name of the element /// The value, or the passed in description, if it was not found public static object GetEnumValue(Type value, string description) { if (value == null) throw new ArgumentNullException("value"); if (description == null) description = string.Empty; if (enumLookup.ContainsKey(value)) { EnumTuple et = enumLookup[value]; if (et.DescriptionToEnum.ContainsKey(description)) return (object)et.DescriptionToEnum[description]; if (et.NameToEnum.ContainsKey(description)) return (object)et.NameToEnum[description]; } FieldInfo[] fis = value.GetFields(); foreach (FieldInfo fi in fis) { DescriptionAttribute[] attributes = (DescriptionAttribute[]) fi.GetCustomAttributes( typeof (DescriptionAttribute), false); if (attributes.Length > 0) { if (attributes[0].Description == description) { return fi.GetValue(fi.Name); } } if (fi.Name == description) { return fi.GetValue(fi.Name); } } throw new InvalidCastException(string.Concat("The received value ", description, " was unrecognized as an ", value.Name, " enum value.")); } } }