Skip to content

Instantly share code, notes, and snippets.

@jpoehls
Created April 7, 2010 13:33
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save jpoehls/358874 to your computer and use it in GitHub Desktop.
Save jpoehls/358874 to your computer and use it in GitHub Desktop.
Enum Helper
using System;
namespace Samples
{
public static class EnumHelper
{
public static string GetDescription(this Enum value)
{
if (value == null)
{
throw new ArgumentNullException("value");
}
string desc = value.ToString();
FieldInfo info = value.GetType().GetField(desc);
var attrs = (DescriptionAttribute[])info.GetCustomAttributes(typeof(DescriptionAttribute), false);
if (attrs != null && attrs.Length > 0)
{
desc = attrs[0].Description;
}
return desc;
}
public static EnumSortMode GetSortMode(Type enumType)
{
if (enumType == null)
{
throw new ArgumentNullException("enumType");
}
if (!enumType.IsEnum)
{
throw new ArgumentException("Target type must be an enum.");
}
EnumSortMode sortMode = EnumSortMode.ByName;
var info = enumType;
var attrs = (EnumSortModeAttribute[])info.GetCustomAttributes(typeof(EnumSortModeAttribute), false);
if (attrs != null && attrs.Length > 0)
{
sortMode = attrs[0].SortMode;
}
return sortMode;
}
public static T Parse<T>(string value) where T : struct
{
return Parse<T>(value, false);
}
public static T Parse<T>(string value, bool ignoreCase) where T : struct
{
if (!typeof(T).IsEnum)
{
throw new ArgumentException("T must be an enum type.");
}
var result = (T)Enum.Parse(typeof(T), value, ignoreCase);
return result;
}
public static T ToEnum<T>(this string value) where T : struct
{
return Parse<T>(value);
}
public static T ToEnum<T>(this string value, bool ignoreCase) where T : struct
{
return Parse<T>(value, ignoreCase);
}
}
}
using System;
namespace Samples
{
public enum EnumSortMode
{
ByName,
ByValue
}
}
using System;
namespace Samples
{
[AttributeUsage(AttributeTargets.Enum)]
public class EnumSortModeAttribute : Attribute
{
public EnumSortModeAttribute(EnumSortMode sortMode)
{
SortMode = sortMode;
}
public EnumSortMode SortMode { get; private set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment