Skip to content

Instantly share code, notes, and snippets.

@robgray
Last active November 24, 2015 00:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robgray/7fee7b2fb7d623ece802 to your computer and use it in GitHub Desktop.
Save robgray/7fee7b2fb7d623ece802 to your computer and use it in GitHub Desktop.
Enum helper classes to present enums in a UI friendly manner (e.g. convert MyEnum to "My Enum" instead of the value "MyEnum", that MyEnum.ToString() would return.
using System;
using System.Collections.Generic;
namespace EnumHelpers
{
[AttributeUsage(AttributeTargets.Enum | AttributeTargets.Field, AllowMultiple = false)]
public sealed class EnumDescriptionAttribute : Attribute
{
public string Description { get; private set; }
public EnumDescriptionAttribute(string description)
{
Description = description;
}
}
/// <summary>
/// This class provides helper functions to enumerations.
/// </summary>
/// <typeparam name="T"></typeparam>
public static class Enum<T>
{
public static T Parse(string value)
{
return (T) Enum.Parse(typeof (T), value);
}
public static T GetEnumFromDescription(string description)
{
var names = Enum.GetNames(typeof (T));
foreach(var value in names)
{
if (value == description) return Parse(value);
var desc = value.ToString();
var fieldInfo = typeof(T).GetField(desc);
var attributes =
(EnumDescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(EnumDescriptionAttribute), false);
if (attributes != null && attributes.Length > 0)
{
if (attributes[0].Description == description)
{
return Parse(value);
}
}
}
throw new ArgumentOutOfRangeException("Cannot translate '" + description + "' into a value from '" +
typeof (T).Name);
}
public static string GetDescription(Enum value)
{
var description = value.ToString();
var fieldInfo = value.GetType().GetField(description);
var attributes =
(EnumDescriptionAttribute[]) fieldInfo.GetCustomAttributes(typeof (EnumDescriptionAttribute), false);
if (attributes != null && attributes.Length > 0)
{
description = attributes[0].Description;
}
return description;
}
public static IList<string> GetAllDescriptions()
{
IList<string> descs = new List<string>();
var enumValues = Enum.GetValues(typeof (T));
foreach (Enum value in enumValues)
{
descs.Add(GetDescription(value));
}
return descs;
}
}
public static class EnumExtension
{
/// <summary>
/// Gets any descriptive value associated with this enum value. Otherwise calls ToString()
/// </summary>
public static string GetDescription<TEnum>(this TEnum e) where TEnum : struct, IConvertible
{
// default to enum ToString but look for something more descriptive.
var description = e.ToString();
var fieldInfo = e.GetType().GetField(description);
try
{
// First look for an XmlEnumAttribute attribute, as these come from WSDL etc
var attributes = (XmlEnumAttribute[]) fieldInfo.GetCustomAttributes(typeof (XmlEnumAttribute), false);
if (attributes != null && attributes.Length > 0)
{
return attributes[0].Name;
}
}
catch (Exception ex) { /* Supress and continue */ }
try
{
var attributes =
(DescriptionAttribute[]) fieldInfo.GetCustomAttributes(typeof (DescriptionAttribute), false);
if (attributes != null && attributes.Length > 0)
{
return attributes[0].Description;
}
}
catch (Exception ex) { /* Supress and continue */ }
try
{
var attributes =
(EnumDescriptionAttribute[]) fieldInfo.GetCustomAttributes(typeof (EnumDescriptionAttribute), false);
if (attributes != null && attributes.Length > 0)
{
return attributes[0].Description;
}
}
catch (Exception ex) { /* Supress and continue */ }
return description;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment