GetEnumDescription

using System; using System.Reflection; using System.ComponentModel; public static class MyExtensionMethods { public static string GetEnumDescription(this object obj) { if (obj == null) { return null; } Type t = obj.GetType(); if (t.IsEnum) { MemberInfo[] memInfo = t.GetMember(obj.ToString()); object[] attributes = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false); if ((attributes != null) && (attributes.Any())) { string description = ((DescriptionAttribute)attributes[0]).Description; return description; } } return null; } }
Simple way to get the content of DescriptionAttribute from an enum item.

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.