Get Enum Description

   
 
//http://facebooktoolkit.codeplex.com/
//http://facebooktoolkit.codeplex.com/license
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Text;

namespace Facebook.Utility
{
  ///<summary>
    /// Contains helper functions used to help when using enums inside of collections and serialization
  ///</summary>
  public class EnumHelper
  {
    private EnumHelper()
    {
    }

    ///<summary>
    ///</summary>
    ///<param name="enumeratedType"></param>
    ///<typeparam name="T"></typeparam>
    ///<returns></returns>
    ///<exception cref="ArgumentException"></exception>
    public static string GetEnumDescription<T>(T enumeratedType)
    {
      var description = enumeratedType.ToString();

      var enumType = typeof (T);
      // Can&#039;t use type constraints on value types, so have to do check like this
      if (enumType.BaseType != typeof (Enum))
        throw new ArgumentException("T must be of type System.Enum");

      var fieldInfo = enumeratedType.GetType().GetField(enumeratedType.ToString());

      if (fieldInfo != null)
      {
        var attributes = fieldInfo.GetCustomAttributes(typeof (DescriptionAttribute), false);

        if (attributes != null &amp;&amp; attributes.Length > 0)
        {
          description = ((DescriptionAttribute) attributes[0]).Description;
        }
      }

      return description;
    }

    ///<summary>
    ///</summary>
    ///<param name="enums"></param>
    ///<typeparam name="T"></typeparam>
    ///<returns></returns>
    ///<exception cref="ArgumentException"></exception>
    public static string GetEnumCollectionDescription<T>(Collection<T> enums)
    {
      var sb = new StringBuilder();

      var enumType = typeof (T);

      // Can&#039;t use type constraints on value types, so have to do check like this
      if (enumType.BaseType != typeof (Enum))
        throw new ArgumentException("T must be of type System.Enum");

      foreach (var enumeratedType in enums)
      {
        sb.AppendLine(GetEnumDescription(enumeratedType));
      }

      return sb.ToString();
    }
  }
}

   
     


Enum To Array

#region License
// (c) Intergen.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace TextGlow.Control.Utilities
{
public static class EnumUtils
{
public static void sdf()
{
}

public static T[] EnumToArray() where T : struct
{
//get the enumeration type
Type et = typeof(T);
T defaultValue = default(T);

//get the public static fields (members of the enum)
FieldInfo[] fi = et.GetFields(BindingFlags.Static | BindingFlags.Public);

//create a new enum array
T[] values = new T[fi.Length];

//populate with the values
for (int i = 0; i < fi.Length; i++) { values[i] = (T)fi[i].GetValue(defaultValue); } //return the array return values; } } } [/csharp]

Try to parse Enum

   
 


namespace System
{
  public static class EnumExtensions
  {
    /// <summary>
    /// Tries the parse.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="theEnum">The enum.</param>
    /// <param name="valueToParse">The value to parse.</param>
    /// <param name="returnValue">The return value.</param>
    /// <returns></returns>
    public static bool TryParse<T>(this Enum theEnum, string valueToParse, out T returnValue)
    {
      returnValue = default(T);
      int intEnumValue;
      if (Int32.TryParse(valueToParse, out intEnumValue))
      {
        if (Enum.IsDefined(typeof (T), intEnumValue))
        {
          returnValue = (T) (object) intEnumValue;
          return true;
        }
      }
      return false;
    }
  }
}

   
     


Wrap an enumerable so that clients can't get to the underlying implementation via a down-case

   
 
        
//******************************
// Written by Peter Golde
// Copyright (c) 2004-2007, Wintellect
//
// Use and restribution of this code is subject to the license agreement 
// contained in the file "License.txt" accompanying this file.
//******************************

using System;
using System.Collections;
using System.Collections.Generic;


namespace Wintellect.PowerCollections
{
  /// <summary>
  /// A holder class for various internal utility functions that need to be shared.
  /// </summary>
    internal static class Util
    {

        /// <summary>
        /// Wrap an enumerable so that clients can&#039;t get to the underlying
        /// implementation via a down-case
        /// </summary>
        /// <param name="wrapped">Enumerable to wrap.</param>
        /// <returns>A wrapper around the enumerable.</returns>
        public static IEnumerable<T> CreateEnumerableWrapper<T>(IEnumerable<T> wrapped)
        {
            return new WrapEnumerable<T>(wrapped);
        }
        /// <summary>
        /// Wrap an enumerable so that clients can&#039;t get to the underlying 
        /// implementation via a down-cast.
        /// </summary>
        class WrapEnumerable<T> : IEnumerable<T>
        {
            IEnumerable<T> wrapped;

            /// <summary>
            /// Create the wrapper around an enumerable.
            /// </summary>
            /// <param name="wrapped">IEnumerable to wrap.</param>
            public WrapEnumerable(IEnumerable<T> wrapped)
            {
                this.wrapped = wrapped;
            }

            public IEnumerator<T> GetEnumerator()
            {
                return wrapped.GetEnumerator();
            }

            IEnumerator IEnumerable.GetEnumerator()
            {
                return ((IEnumerable)wrapped).GetEnumerator();
            }
        }
   }
}        

   
     


Format enum value

   
  

using System;

enum Color {Yellow = 1, Blue, Green};

class Sample 
{

    public static void Main() 
    {

    Console.WriteLine("Standard Enumeration Format Specifiers");
    Console.WriteLine(
        "(G) General:. . . . . . . . . {0:G}
" +
        "    (default):. . . . . . . . {0} (default = &#039;G&#039;)
" +
        "(F) Flags:. . . . . . . . . . {0:F} (flags or integer)
" +
        "(D) Decimal number: . . . . . {0:D}
" +
        "(X) Hexadecimal:. . . . . . . {0:X}
", 
        Color.Green);       


    }
}

   
    
     


switch statement with Enum and call its ToString function

   
   

using System;

enum EmpType : byte {
    Manager = 10,
    Grunt = 1,
    Contractor = 100,
    VP = 9
}

class Program {
    public static void AskForBonus(EmpType e) {
        switch (e) {
            case EmpType.Contractor:
                Console.WriteLine("1");
                break;
            case EmpType.Grunt:
                Console.WriteLine("2");
                break;
            case EmpType.Manager:
                Console.WriteLine("3");
                break;
            case EmpType.VP:
                Console.WriteLine("4");
                break;
            default: break;
        }
    }


    static void Main(string[] args) {
        EmpType fred;
        fred = EmpType.VP;
        AskForBonus(fred);
        Console.WriteLine(fred.ToString());

    }
}