using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Reflection;
using System.Text;
namespace LinkedIn.Utility
{
///
/// A helper class for enums.
///
public static class EnumHelper
{
/// usually int
public static List GetValues()
{
List values = new List();
Array array = Enum.GetValues(typeof(TEnum));
foreach (TValue item in array)
{
values.Add(item);
}
return values;
}
///
/// Get the description of a value.
///
/// The value.
/// A description of the value.
public static string GetDescription(Enum value)
{
FieldInfo fieldInfo = value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes =
(DescriptionAttribute[])fieldInfo.GetCustomAttributes(
typeof(DescriptionAttribute), false);
return (attributes.Length > 0) ? attributes[0].Description : value.ToString();
}
///
///
///
///
///
///
public static bool HasFlag(this TEnum enumeratedType, TEnum value)
where TEnum : struct, IComparable, IFormattable, IConvertible
{
if ((enumeratedType is Enum) == false)
{
throw new InvalidOperationException("Struct is not an Enum.");
}
if (typeof(TEnum).GetCustomAttributes(
typeof(FlagsAttribute), false).Length == 0)
{
throw new InvalidOperationException("Enum must use [Flags].");
}
long enumValue = enumeratedType.ToInt64(CultureInfo.InvariantCulture);
long flagValue = value.ToInt64(CultureInfo.InvariantCulture);
if ((enumValue & flagValue) == flagValue)
{
return true;
}
return false;
}
}
}