Return to Snippet

Revision: 36181
at November 19, 2010 03:50 by dotNetkow


Updated Code
using System.ComponentModel;

public class MyClass
{
  /// <summary>
  /// Returns the description attribute of an Enum if available, othewise returns
  /// the toString() of the value passed in.
  ///
  /// Useful for Enums with spaces in them.
  /// </summary>
  /// <param name="value">the enum</param>
  /// <returns>the description string of the enum</returns>
  public static string GetEnumDescription(Enum value)
  {
    FieldInfo fi = value.GetType().GetField(value.ToString());

    DescriptionAttribute[] attributes =
	(DescriptionAttribute[])fi.GetCustomAttributes(
		typeof(DescriptionAttribute),false);

    if (attributes != null && attributes.Length > 0)
	return attributes[0].Description;
    else
	return value.ToString();
    }
}

public enum Types
{
  [Description("Created Date")]
  CreatedDate,
  Description,
  Test
}

Revision: 36180
at November 19, 2010 03:11 by dotNetkow


Updated Code
using System.ComponentModel.DescriptionAttribute;

public class MyClass
{
  /// <summary>
  /// Returns the description attribute of an Enum if available, othewise returns
  /// the toString() of the value passed in.
  ///
  /// Useful for Enums with spaces in them.
  /// </summary>
  /// <param name="value">the enum</param>
  /// <returns>the description string of the enum</returns>
  public static string GetEnumDescription(Enum value)
  {
    FieldInfo fi = value.GetType().GetField(value.ToString());

    DescriptionAttribute[] attributes =
	(DescriptionAttribute[])fi.GetCustomAttributes(
		typeof(DescriptionAttribute),false);

    if (attributes != null && attributes.Length > 0)
	return attributes[0].Description;
    else
	return value.ToString();
    }
}

public enum Types
{
  [Description("Created Date")]
  CreatedDate,
  Description,
  Test
}

Revision: 36179
at November 19, 2010 02:58 by dotNetkow


Updated Code
using System.ComponentModel;

public class MyClass
{
  /// <summary>
  /// Returns the description attribute of an Enum if available, othewise returns
  /// the toString() of the value passed in.
  ///
  /// Useful for Enums with spaces in them.
  /// </summary>
  /// <param name="value">the enum</param>
  /// <returns>the description string of the enum</returns>
  public static string GetEnumDescription(Enum value)
  {
    FieldInfo fi = value.GetType().GetField(value.ToString());

    DescriptionAttribute[] attributes =
	(DescriptionAttribute[])fi.GetCustomAttributes(
		typeof(DescriptionAttribute),false);

    if (attributes != null && attributes.Length > 0)
	return attributes[0].Description;
    else
	return value.ToString();
    }
}

public enum Types
{
  [Description("Created Date")]
  CreatedDate,
  Description,
  Test
}

Revision: 36178
at November 19, 2010 02:55 by dotNetkow


Initial Code
using System.ComponentModel;

public class MyClass
{
/// <summary>
		/// Returns the description attribute of an Enum if available, othewise returns
		/// the toString() of the value passed in.
		///
		/// Useful for Enums with spaces in them.
		/// </summary>
		/// <param name="value">the enum</param>
		/// <returns>the description string of the enum</returns>
		public static string GetEnumDescription(Enum value)
		{
			FieldInfo fi = value.GetType().GetField(value.ToString());

			DescriptionAttribute[] attributes =
					(DescriptionAttribute[])fi.GetCustomAttributes(
					typeof(DescriptionAttribute),
					false);

			if (attributes != null &&
					attributes.Length > 0)
				return attributes[0].Description;
			else
				return value.ToString();
		}


}

public enum Types
{
  [Description("Created Date")]
  CreatedDate,
  Description,
  Test
}

Initial URL


Initial Description
By default, enum values cannot have spaces in them (i.e. "Created Date" can only be "CreatedDate").  With this method, we can change that!

Initial Title
Allow enums to have spaces

Initial Tags
c#

Initial Language
C#