using System; using System.Collections; using System.ComponentModel; using System.Globalization; using System.Reflection; using System.Text; namespace SPSolutions.SharePoint.SoftwareSite { public class EnumUtil { public static string GetDisplayName(T enumValue) where T : struct { Type typeFromHandle = typeof(T); if (!typeFromHandle.IsEnum) { return null; } string text = enumValue.ToString(); FieldInfo field = typeFromHandle.GetField(text); if (field == null) { return text; } object[] customAttributes = field.GetCustomAttributes(typeof(DescriptionAttribute), false); if (customAttributes == null || customAttributes.Length == 0) { return text; } DescriptionAttribute descriptionAttribute = customAttributes[0] as DescriptionAttribute; if (descriptionAttribute == null) { return text; } return descriptionAttribute.Description; } public static string GetDisplayNames(T enumValues) where T : struct { StringBuilder stringBuilder = new StringBuilder(); Type typeFromHandle = typeof(T); if (!typeFromHandle.IsEnum) { return null; } ulong num = EnumUtil.ToUInt64(enumValues); IEnumerator enumerator = Enum.GetValues(typeFromHandle).GetEnumerator(); try { while (enumerator.MoveNext()) { T t = (T)((object)enumerator.Current); ulong num2 = EnumUtil.ToUInt64(t); if ((num & num2) == num2) { string displayName = EnumUtil.GetDisplayName(t); if (stringBuilder.Length != 0) { stringBuilder.Append(", "); } stringBuilder.Append(displayName); } } } finally { IDisposable disposable = enumerator as IDisposable; if (disposable != null) { disposable.Dispose(); } } return stringBuilder.ToString(); } private static ulong ToUInt64(object value) { switch (Convert.GetTypeCode(value)) { case TypeCode.SByte: case TypeCode.Int16: case TypeCode.Int32: case TypeCode.Int64: return (ulong)Convert.ToInt64(value, CultureInfo.InvariantCulture); case TypeCode.Byte: case TypeCode.UInt16: case TypeCode.UInt32: case TypeCode.UInt64: return Convert.ToUInt64(value, CultureInfo.InvariantCulture); default: throw new InvalidOperationException("Unknown enumeration type"); } } } }