public static bool AreValuesUnique<EnumType, ValueType>()
{
List<ValueType> valuesList = GetValues<EnumType, ValueType>();
return CollectionsHelper.AreValuesUnique<ValueType>(valuesList);
}
///<typeparam name="ValueType">usually int</typeparam>
public static List<ValueType> FindDuplicates<EnumType, ValueType>()
{
List<ValueType> valuesList = GetValues<EnumType, ValueType>();
return CollectionsHelper.FindDuplicates<ValueType>(valuesList);
}
// use Enum Description attribute to specify description
// http://www.codeproject.com/csharp/enumwithdescription.asp
//Also consider use ResourceManager see http://www.codeproject.com/csharp/LocalizingEnums.asp?print=true
public static string GetDescription(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();
}
// http://rodenbaugh.net/files/folders/18/download.aspx has generic class Enum<T> with a lot of interesting methods.
// examples of use see http://rodenbaugh.net/archive/2006/11/01/Enum-Helper-Class-Using-Generics.aspx
//TODO consider to use if required
#region "Chars to Enums"
//Not used in TSA at the moment
public static char ValueAsChar<TEnum>(TEnum value) where TEnum : struct //ideally enum
{
ThrowExceptionIfNotEnum<TEnum>();
char ch = Convert.ToChar(Convert.ToInt16(value));// Strings.Chr Convert.ToChar(AppType);
return ch;
}
public static TEnum CharAsciiAsEnum<TEnum>(string schValue, TEnum enDefault) where TEnum : struct //ideally enum
{
ThrowExceptionIfNotEnum<TEnum>();
TEnum enum1 = enDefault;
if ((schValue != null) && (schValue.Length == 1))
{
enum1 = CharAsciiAsEnum<TEnum>(schValue[0], enDefault);
}
return enum1;
}
public static TEnum CharAsciiAsEnum<TEnum>(char chValue, TEnum enDefault) where TEnum : struct //ideally enum
{
ThrowExceptionIfNotEnum<TEnum>();
TEnum enum1 = enDefault;
short num1 = (short)Microsoft.VisualBasic.Strings.Asc(chValue);
if (Enum.IsDefined(typeof(TEnum), num1))
{
enum1 = (TEnum)Enum.ToObject(typeof(TEnum), num1);
}
return enum1;
}
#endregion //"Chars to Enums"
#region private methods
private static void ThrowExceptionIfNotEnum<TEnum>()
{
if (!typeof(TEnum).IsEnum)
{
throw new ArgumentException(typeof(TEnum).ToString() + " is not an Enum");
}
}
#endregion //private methods