using System; using System.IO; using System.Text; using System.Collections; using System.Collections.Generic; using System.Reflection; using System.ComponentModel; namespace Ease.Core { public sealed class EnumHelper { private EnumHelper() { } /// /// Use EnumFormatter.ToString() instead. /// /// /// [Obsolete("Use EnumFormatter.ToString()")] public static string GetDescription(Enum value) { return EnumFormatter.ToString(value); } } /// /// Provides formatting functions to convert enumeration values /// to user-friendly strings by breaking proper-case enumeration /// name into seperate words. Use the ToString() method to get /// user-friendly text. Use the Parse method to parse a user-firendly /// text to an enumeration value. /// public sealed class EnumFormatter { private static Dictionary _hashEnums; static EnumFormatter() { _hashEnums = new Dictionary(); } /// /// convert enumeration values to user-friendly string /// by breaking proper-case enumeration /// name into seperate words. RecceivedFromOtherDepot = "Recceived From Other Depot". /// /// The value to be converted /// Formatted string public static string ToString(Enum value) { string s = value.ToString(); StringBuilder desc = new StringBuilder(s.Length); for (int i = 0; i < s.Length; i++) { if (!char.IsLower(s[i]) && i > 0 && char.IsLower(s[i - 1])) { desc.Append(" "); } desc.Append(s[i]); } return desc.ToString(); } /// /// Parse a formatted enumeration name into the value. /// "Recceived From Other Depot" --> RecceivedFromOtherDepot. /// Throws FormattingException if fails to convert to /// an enumeration value for the specified type. /// /// The type to convert to. /// Text to parse /// public static Enum Parse(Type enumType, string text) { text = text.Replace(" ", ""); return (Enum)Enum.Parse(enumType, text); } /// /// Return the actual value in integer. /// /// From based on which enum. /// Value to be converted. /// Integer value. public static int GetValue(Type enumType, Enum value) { return (Convert.ToInt32(Enum.Format(enumType, value, "d"))); } /// /// Return the integer value. /// /// From based on which enum. /// Friendly name or actual name. /// Return an integer value. public static int GetValue(Type enumType, string text) { text = text.Replace(" ", ""); return Convert.ToInt32(Enum.Parse(enumType, text)); } /// /// Return the nteger value index zero based. /// /// From based on which enum. /// Enum value. /// Return an integer index. public static int GetIndex(Type enumType, Enum value) { string s = value.ToString(); return GetIndex(enumType, s); } /// /// Return the nteger value index zero based. /// /// From based on which enum. /// Friendly name or actual name. /// Return an integer index. public static int GetIndex(Type enumType, string text) { if (_hashEnums.ContainsKey(enumType.FullName)) { EnumItemCollection items = _hashEnums[enumType.FullName]; if (items != null) { text = text.Replace(" ", ""); EnumItem item = items[text]; if (item != null) return item.Index; } } return -1; } /// /// Return string array of enum. /// /// From based on which enum. /// Array of string. public static string[] GetNames(Type enumType) { EnumItemCollection items = EnumFormatter.GetObjects(enumType); if (items != null) return items.GetNames(); return new string[] { }; } /// /// Return EnumItemCollection object. /// /// From based on which enum. /// Return EnumItemCollection object. public static EnumItemCollection GetObjects(Type enumType) { EnumItemCollection items; if (_hashEnums.ContainsKey(enumType.FullName)) { items = (EnumItemCollection)_hashEnums[enumType.FullName]; if (items != null) return items; } int index = 0; string[] names = Enum.GetNames(enumType); items = new EnumItemCollection(); foreach (string name in names) { string s = name; StringBuilder desc = new StringBuilder(s.Length); for (int i = 0; i < s.Length; i++) { if (!char.IsLower(s[i]) && i > 0 && char.IsLower(s[i - 1])) { desc.Append(" "); } desc.Append(s[i]); } s = desc.ToString(); EnumItem item = new EnumItem(index, name, s, EnumFormatter.GetValue(enumType, name)); items.Add(item); index++; } _hashEnums.Add(enumType.FullName, items); return items; } /// /// Return EnumItemCollection object. /// /// From based on which enum. /// Array of enums value. /// Return EnumItemCollection object. public static EnumItemCollection GetObjects(Type enumType, Array values) { EnumItemCollection items; #region From Cached Items if (_hashEnums.ContainsKey(enumType.FullName)) { EnumItemCollection cachedItems = (EnumItemCollection)_hashEnums[enumType.FullName]; if (cachedItems != null) { items = new EnumItemCollection(); foreach (EnumItem cachedItem in cachedItems) { foreach (Enum value in values) { int enumValue = Convert.ToInt32(Enum.Format(enumType, value, "d")); if (cachedItem.Value == enumValue) { EnumItem item = new EnumItem(cachedItem.Index, cachedItem.Name, cachedItem.FriendlyName, cachedItem.Value); items.Add(item); break; } } } return items; } } #endregion #region Items agains Passed enums array string[] names = Enum.GetNames(enumType); items = new EnumItemCollection(); foreach (string name in names) { string s = name; StringBuilder desc = new StringBuilder(s.Length); for (int i = 0; i < s.Length; i++) { if (!char.IsLower(s[i]) && i > 0 && char.IsLower(s[i - 1])) { desc.Append(" "); } desc.Append(s[i]); } s = desc.ToString(); int enumValue = Convert.ToInt32(Enum.Parse(enumType, name, true)); foreach (Enum value in values) { int enumParamValue = Convert.ToInt32(Enum.Format(enumType, value, "d")); if (enumValue == enumParamValue) { EnumItem item = new EnumItem(items.Count, name, s, enumValue); items.Add(item); break; } } } #endregion return items; } } #region Helper Class public sealed class EnumItem { #region Constructor internal EnumItem(int index, string name, string friendlyName, int value) { _name = name; _index = index; _value = value; _friendlyName = friendlyName; } #endregion #region Properies #region Property Index : int private readonly int _index; public int Index { get { return _index; } } #endregion #region Property Name : string private readonly string _name; public string Name { get { return _name; } } #endregion #region Property FriendlyName : string private readonly string _friendlyName; public string FriendlyName { get { return _friendlyName; } } #endregion #region Property Value : int private readonly int _value; public int Value { get { return _value; } } #endregion #endregion #region Functions /// /// Use to create an EnumItem object /// /// Type of Enum i.e TypeOf(UserTypeEnum) /// Value of enum i.e UserTypeEnum.Admin /// Returns an instatnce of EnumItem public static EnumItem FromValue(Type enumType, Enum value) { int enumValue = Convert.ToInt32(Enum.Format(enumType, value, "d")); return EnumItem.FromValue(enumType, enumValue); } /// /// Use to create an EnumItem object /// /// Type of Enum i.e TypeOf(UserTypeEnum) /// Valid enum value in int /// Returns an instatnce of EnumItem public static EnumItem FromValue(Type enumType, int value) { int idx = -1; EnumItem item = null; string[] names = Enum.GetNames(enumType); foreach (string name in names) { idx += 1; int val = Convert.ToInt32(Enum.Parse(enumType, name, true)); if (val != value) continue; string s = name; StringBuilder desc = new StringBuilder(s.Length); for (int i = 0; i < s.Length; i++) { if (!char.IsLower(s[i]) && i > 0 && char.IsLower(s[i - 1])) desc.Append(" "); desc.Append(s[i]); } s = desc.ToString(); item = new EnumItem(idx, name, s, value); break; } return item; } /// /// Use to create an EnumItem object /// /// Type of Enum i.e TypeOf(UserTypeEnum) /// Valid value of enum in string i.e Admin /// Returns an instatnce of EnumItem public static EnumItem FromValue(Type enumType, string value) { int enumValue = 0; if (!int.TryParse(value, out enumValue)) enumValue = -99999; if (enumValue == -99999) throw new Exception(string.Format("Invalid use of value: {0}", value)); return EnumItem.FromValue(enumType, enumValue); } /// /// Use to create an EnumItem object /// /// Type of Enum i.e TypeOf(UserTypeEnum) /// Custom arry of enum i.e new UserTypeEnum[]{UserTypeEnum.SuperUser, UserTypeEnum.Admin} /// Value of enum i.e UserTypeEnum.Admin /// Returns an instatnce of EnumItem public static EnumItem FromValue(Type enumType, Array values, Enum value) { int idx = -1; EnumItem item = null; int enumValue = Convert.ToInt32(Enum.Format(enumType, value, "d")); foreach (Enum arValue in values) { idx += 1; string name = arValue.ToString(); int val = Convert.ToInt32(Enum.Parse(enumType, name, true)); if (val != enumValue) continue; string s = name; StringBuilder desc = new StringBuilder(s.Length); for (int i = 0; i < s.Length; i++) { if (!char.IsLower(s[i]) && i > 0 && char.IsLower(s[i - 1])) desc.Append(" "); desc.Append(s[i]); } s = desc.ToString(); item = new EnumItem(idx, name, s, enumValue); break; } if (item == null) return new EnumItem(-1, "Unknown", "Unknown", enumValue); return item; } #endregion } public sealed class EnumItemCollection : CollectionBase { #region Constructor private Hashtable _hashTable; internal EnumItemCollection() { _hashTable = new Hashtable(); InnerList.Clear(); } #endregion #region Functions internal void Add(EnumItem item) { if (!_hashTable.ContainsKey(item.Name)) { _hashTable.Add(item.Name, item); InnerList.Add(item); } } public EnumItem this[Enum value] { get { string s = value.ToString(); return this[s]; } } public EnumItem this[string valueMember] { get { valueMember = valueMember.Replace(" ", ""); if (_hashTable.ContainsKey(valueMember)) return (EnumItem)_hashTable[valueMember]; return null; } } public EnumItem this[int index] { get { return (EnumItem)InnerList[index]; } } internal string[] GetNames() { string[] names = null; if (this.Count > 0) { names = new string[this.Count]; for (int index = 0; index < this.Count; index++) { names[index] = this[index].FriendlyName; } } return names; } #endregion } public class EnumDescription { public static string GetEnumDescription(Enum value) { FieldInfo fi = value.GetType().GetField(value.ToString()); DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes (typeof(DescriptionAttribute), false); return (attributes.Length > 0) ? attributes[0].Description : value.ToString(); } } #endregion }