Object - GetCustomAttributeDescription
An object extension method that gets description attribute.
public static void Main() { var thisClass = new MyTestClass(); var thisEnum = MyTestEnum.Test; //C# Extension Method: Object - GetCustomAttributeDescription var result1 = thisClass.GetCustomAttributeDescription(); var result2 = thisClass.GetCustomAttributeDescription(false); var result3 = thisClass.GetType().GetCustomAttributeDescription(); var result4 = thisClass.GetType().GetCustomAttributeDescription(false); var result5 = thisEnum.GetCustomAttributeDescription(); var result6 = thisEnum.GetCustomAttributeDescription(false); Console.WriteLine(result1); Console.WriteLine(result2); Console.WriteLine(result3); Console.WriteLine(result4); Console.WriteLine(result5); Console.WriteLine(result6); } } [Description("Test Description")] public class MyTestClass { } public enum MyTestEnum { [Description("Test Description")] Test
View Source
using System; using System.ComponentModel; using System.Reflection; public static partial class Extensions { /// <summary> /// An object extension method that gets description attribute. /// </summary> /// <param name="value">The value to act on.</param> /// <returns>The description attribute.</returns> public static string GetCustomAttributeDescription(this object value) { var type = value.GetType(); var attributes = type.IsEnum ? type.GetField(value.ToString()).GetCustomAttributes(typeof (DescriptionAttribute)) : type.GetCustomAttributes(typeof (DescriptionAttribute)); if (attributes.Length == 0) { return null; } if (attributes.Length == 1) { return ((DescriptionAttribute) attributes[0]).Description; } throw new Exception(string.Format("Ambiguous attribute. Multiple custom attributes of the same type found: {0} attributes found.", attributes.Length)); } /// <summary>An object extension method that gets description attribute.</summary> /// <param name="value">The value to act on.</param> /// <param name="inherit">true to inherit.</param> /// <returns>The description attribute.</returns> public static string GetCustomAttributeDescription(this object value, bool inherit) { var type = value.GetType(); var attributes = type.IsEnum ? type.GetField(value.ToString()).GetCustomAttributes(typeof (DescriptionAttribute), inherit) : type.GetCustomAttributes(typeof (DescriptionAttribute)); if (attributes.Length == 0) { return null; } if (attributes.Length == 1) { return ((DescriptionAttribute) attributes[0]).Description; } throw new Exception(string.Format("Ambiguous attribute. Multiple custom attributes of the same type found: {0} attributes found.", attributes.Length)); } /// <summary>An object extension method that gets description attribute.</summary> /// <param name="value">The value to act on.</param> /// <returns>The description attribute.</returns> public static string GetCustomAttributeDescription(this MemberInfo value) { var attributes = value.GetCustomAttributes(typeof (DescriptionAttribute)); if (attributes.Length == 0) { return null; } if (attributes.Length == 1) { return ((DescriptionAttribute) attributes[0]).Description; } throw new Exception(string.Format("Ambiguous attribute. Multiple custom attributes of the same type found: {0} attributes found.", attributes.Length)); } /// <summary>An object extension method that gets description attribute.</summary> /// <param name="value">The value to act on.</param> /// <param name="inherit">true to inherit.</param> /// <returns>The description attribute.</returns> public static string GetCustomAttributeDescription(this MemberInfo value, bool inherit) { var attributes = value.GetCustomAttributes(typeof (DescriptionAttribute), inherit); if (attributes.Length == 0) { return null; } if (attributes.Length == 1) { return ((DescriptionAttribute) attributes[0]).Description; } throw new Exception(string.Format("Ambiguous attribute. Multiple custom attributes of the same type found: {0} attributes found.", attributes.Length)); } }