using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Reflection; using System.Linq; using System.Diagnostics; using System.Threading; using System.ComponentModel; using System.Web.Mvc; namespace Sleis.Utility { /// /// Basic helper functions for dealing with enums. /// public static class EnumUtility { public static IList GetEnumFlagsArray(T inValue) where T : struct, IConvertible { if (!typeof(T).IsEnum) { throw new ArgumentException(typeof(T).ToString() + " is not an Enum"); } T[] possibleValues = (T[])Enum.GetValues(typeof(T)); List list = new List(); long testValue = Convert.ToInt64(inValue); foreach (T possibleValue in possibleValues) { long possibleTestValue = Convert.ToInt64(possibleValue); if (possibleTestValue != 0) { if ((possibleTestValue & testValue) != 0) { list.Add(possibleValue); } } else if (testValue == 0) { // This is the case where we have an "UNDEFINED" value that equals 0 (i.e., no flags set) list.Add(possibleValue); break; } } return list; } public static T ParseEnum(string value) where T : struct, IConvertible { return ParseEnum(value, true); } public static T ParseEnum(string value, bool ignoreCase) where T : struct, IConvertible { if (!typeof(T).IsEnum) { throw new ArgumentException(typeof(T).ToString() + " is not an Enum.", "T", null); } T result; if (!string.IsNullOrEmpty(value)) { try { result = (T)Enum.Parse(typeof(T), value, ignoreCase); } catch (Exception) { result = default(T); } } else { result = default(T); } return result; } public static SelectList CreateSelectList() { var values = from TEnum e in Enum.GetValues(typeof(TEnum)) select new { ID = e, Name = e.ToString() }; return new SelectList(values, "ID", "Name"); } public static SelectList CreateSelectList(TEnum selectedVal) { var values = from TEnum e in Enum.GetValues(typeof(TEnum)) select new { ID = e, Name = e.ToString() }; return new SelectList(values, "ID", "Name", selectedVal); } /// /// Create a select list from the Enum /// /// /// /// public static SelectList ToSelectList(this TEnum enumObj) where TEnum : struct, IConvertible { var values = from TEnum e in Enum.GetValues(typeof(TEnum)) select new { ID = e, Name = e.ToString() }; return new SelectList(values, "Id", "Name", enumObj); } /// /// Return the [Description] attribute value for an enum value, if specified. /// public static string ToDescription(T enumValue) where T : struct, IConvertible { if (!enumValue.GetType().IsEnum) { throw new ArgumentException("enumValue must be an enum"); } if (!typeof(T).IsEnum) { throw new InvalidOperationException("T must be an enum"); } DescriptionAttribute[] da = (DescriptionAttribute[]) (typeof(T).GetField(enumValue.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), false)); return ((da.Length > 0) ? da[0].Description : enumValue.ToString()); } /// /// Return the enum value from a [Description] attribute. /// public static T FromDescription(string enumDescription) where T : struct, IConvertible { T value; FromDescription(enumDescription, out value); return value; } /// /// Return the enum value from a [Description] attribute. /// public static bool FromDescription(string enumDescription, out T value) where T : struct, IConvertible { if (!typeof(T).IsEnum) { throw new InvalidOperationException("T must be an enum"); } foreach (FieldInfo fieldInfo in typeof(T).GetFields()) { DescriptionAttribute[] da = (DescriptionAttribute[]) fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false); if ((da.Length > 0) && string.Equals(da[0].Description, enumDescription, StringComparison.InvariantCultureIgnoreCase)) { value = (T)fieldInfo.GetValue(null); return true; } } value = default(T); return false; } /// /// Return the enum value from a [Description] attribute. /// public static T FlagsFromDescriptions(string enumDescription, char separatorChar) where T : struct, IConvertible { if (!typeof(T).IsEnum) { throw new InvalidOperationException("T must be an enum"); } T rtnValue = default(T); if (!string.IsNullOrEmpty(enumDescription)) { string[] values = enumDescription.Split(separatorChar); foreach (string value in values) { string enumValueStr = value.Trim(); if ( !string.IsNullOrEmpty(enumValueStr) ) { T enumValue = FromDescription(enumValueStr); rtnValue = SetFlag(rtnValue, enumValue); } } } return rtnValue; } /// /// Return a collection of descriptions for all enum values that have a /// [Description] attribute specified. /// public static ICollection GetAllDescriptions() where T : struct, IConvertible { if (!typeof(T).IsEnum) { throw new InvalidOperationException("T must be an enum"); } List list = new List(); foreach (FieldInfo fieldInfo in typeof(T).GetFields()) { DescriptionAttribute[] da = (DescriptionAttribute[]) fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false); if (da.Length > 0) { list.Add(da[0].Description); } } return list; } public static string GetDefaultDescriptionAsString(ICollection list) where T : struct, IConvertible { if (!typeof(T).IsEnum) { throw new InvalidOperationException("T must be an enum"); } string roleName = String.Empty; if (list != null && list.Count > 0) { List names = new List(); foreach (T key in list) { List descs = new List(EnumUtility.GetAllDescriptions(key)); if (descs.Count > 0 && !names.Contains(descs[0])) { names.Add(descs[0]); } } roleName = String.Join(", ", names.ToArray()); } return roleName; } /// /// Return a collection of descriptions for all enum values that have a /// [Description] attribute specified. /// public static ICollection GetAllDescriptions(T enumValue) where T : struct, IConvertible { if (!typeof(T).IsEnum) { throw new InvalidOperationException("T must be an enum"); } bool isFlags = typeof(T).IsDefined(typeof(FlagsAttribute), true); List list = new List(); long flagValue = 0; if (isFlags) { flagValue = (long)Convert.ChangeType(enumValue, typeof(long)); } foreach (FieldInfo fieldInfo in typeof(T).GetFields()) { try { DescriptionAttribute[] da = (DescriptionAttribute[]) fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false); if (da.Length > 0) { T fieldValue = (T) fieldInfo.GetValue(null); if (isFlags) { long fieldFlagValue = (long) Convert.ChangeType(fieldValue, typeof(long)); if ( (fieldFlagValue & flagValue) == fieldFlagValue ) { list.Add(da[0].Description); } } else if (fieldValue.Equals(enumValue)) { list.Add(da[0].Description); break; } } } catch (Exception) { } } return list; } /// /// T should be a flags enum. This method sets the flag flagToSet on currentFlags and returns /// the result. /// public static T SetFlag(T currentFlags, T flagToSet) where T : struct, IConvertible { if (!typeof(T).IsEnum) { throw new InvalidOperationException("T must be an enum"); } return (T)Enum.ToObject(typeof(T), ((IConvertible)currentFlags).ToInt32(null) | ((IConvertible)flagToSet).ToInt32(null)); } public static T ClearFlag(T currentFlags, T flagToClear) where T : struct, IConvertible { if (!typeof(T).IsEnum) { throw new InvalidOperationException("T must be an enum"); } return (T)Enum.ToObject(typeof(T), ((IConvertible)currentFlags).ToInt32(null) & (~((IConvertible)flagToClear).ToInt32(null))); } public static T AssignFlag(T currentFlags, T flagToAssign, bool doSet) where T : struct, IConvertible { if (doSet) { return SetFlag(currentFlags, flagToAssign); } else { return ClearFlag(currentFlags, flagToAssign); } } public static bool IsFlagSet(T currentFlags, T flagToCheck) where T : struct, IConvertible { if (!typeof(T).IsEnum) { throw new InvalidOperationException("T must be an enum"); } return ((((IConvertible)currentFlags).ToInt32(null) & ((IConvertible)flagToCheck).ToInt32(null)) != 0); } public static int GetLargestEnumStringSize(Type enumType) { if (!enumType.IsEnum) { throw new InvalidOperationException("enumType must be an enum"); } string[] names = Enum.GetNames(enumType); int maxSize = 0; foreach (string name in names) { if (maxSize < name.Length) { maxSize = name.Length; } } return maxSize; } } }