andrew4582

Enum Extensions GetDescription

Aug 1st, 2014
361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.70 KB | None | 0 0
  1. using System.ComponentModel;
  2. using System.Reflection;
  3.  
  4. public static class EnumExtensions
  5.     {
  6.         public static string GetDescription(this Enum enumeration)
  7.         {
  8.             Type  enumType = enumeration.GetType();
  9.             object value = (object)enumeration;
  10.             System.Reflection.MemberInfo[] memberInfo = enumType.GetMember(Enum.GetName(enumType, value));
  11.             System.ComponentModel.DescriptionAttribute attribute = (System.ComponentModel.DescriptionAttribute)memberInfo[0].GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false).First();
  12.  
  13.             string description = attribute.Description;
  14.  
  15.             return description;
  16.         }
  17.     }
Advertisement
Add Comment
Please, Sign In to add comment