Object - GetCustomAttributeByFullName
A MemberInfo extension method that gets custom attribute by full name.
public static void Main() { var thisClass = new MyTestClass(); var thisEnum = MyTestEnum.Test; //C# Extension Method: Object - GetCustomAttributeByFullName var result1 = thisClass.GetCustomAttributeByFullName("MyTestAttribute"); var result2 = thisClass.GetCustomAttributeByFullName("MyTestAttribute", false); var result3 = thisClass.GetType().GetCustomAttributeByFullName("MyTestAttribute"); var result4 = thisClass.GetType().GetCustomAttributeByFullName("MyTestAttribute", false); var result5 = thisEnum.GetCustomAttributeByFullName("MyTestAttribute"); var result6 = thisEnum.GetCustomAttributeByFullName("MyTestAttribute", false); Console.WriteLine(((MyTestAttribute) result1).Text); Console.WriteLine(((MyTestAttribute) result2).Text); Console.WriteLine(((MyTestAttribute) result3).Text); Console.WriteLine(((MyTestAttribute) result4).Text); Console.WriteLine(((MyTestAttribute) result5).Text); Console.WriteLine(((MyTestAttribute) result6).Text); } } [AttributeUsage(AttributeTargets.All, AllowMultiple = true)] public class MyTestAttribute : Attribute { public string Text; public MyTestAttribute(string text) { Text = text; } } [MyTest("Test Class")] public class MyTestClass { } public enum MyTestEnum { [MyTest("Test Enum")] Test
View Source
using System; using System.Linq; using System.Reflection; public static partial class Extensions { /// <summary>A MemberInfo extension method that gets custom attribute by full name.</summary> /// <exception cref="Exception">Thrown when an exception error condition occurs.</exception> /// <param name="this">The @this to act on.</param> /// <param name="fullName">Name of the full.</param> /// <returns>The custom attribute by full name.</returns> public static object GetCustomAttributeByFullName(this object @this, string fullName) { var type = @this.GetType(); var attributes = type.IsEnum ? type.GetField(@this.ToString()).GetCustomAttributes().Where(x => x.GetType().FullName == fullName).ToArray() : type.GetCustomAttributes().Where(x => x.GetType().FullName == fullName).ToArray(); if (attributes.Length == 0) { return null; } if (attributes.Length == 1) { return attributes[0]; } throw new Exception(string.Format("Ambiguous attribute. Multiple custom attributes of the same type found: {0} attributes found.", attributes.Length)); } /// <summary>A MemberInfo extension method that gets custom attribute by full name.</summary> /// <exception cref="Exception">Thrown when an exception error condition occurs.</exception> /// <param name="this">The @this to act on.</param> /// <param name="fullName">Name of the full.</param> /// <param name="inherit">true to inherit.</param> /// <returns>The custom attribute by full name.</returns> public static object GetCustomAttributeByFullName(this object @this, string fullName, bool inherit) { var type = @this.GetType(); var attributes = type.IsEnum ? type.GetField(@this.ToString()).GetCustomAttributes(inherit).Where(x => x.GetType().FullName == fullName).ToArray() : type.GetCustomAttributes(inherit).Where(x => x.GetType().FullName == fullName).ToArray(); if (attributes.Length == 0) { return null; } if (attributes.Length == 1) { return attributes[0]; } throw new Exception(string.Format("Ambiguous attribute. Multiple custom attributes of the same type found: {0} attributes found.", attributes.Length)); } /// <summary>A MemberInfo extension method that gets custom attribute by full name.</summary> /// <exception cref="Exception">Thrown when an exception error condition occurs.</exception> /// <param name="this">The @this to act on.</param> /// <param name="fullName">Name of the full.</param> /// <returns>The custom attribute by full name.</returns> public static object GetCustomAttributeByFullName(this MemberInfo @this, string fullName) { var attributes = @this.GetCustomAttributes().Where(x => x.GetType().FullName == fullName).ToArray(); if (attributes.Length == 0) { return null; } if (attributes.Length == 1) { return attributes[0]; } throw new Exception(string.Format("Ambiguous attribute. Multiple custom attributes of the same type found: {0} attributes found.", attributes.Length)); } /// <summary>A MemberInfo extension method that gets custom attribute by full name.</summary> /// <exception cref="Exception">Thrown when an exception error condition occurs.</exception> /// <param name="this">The @this to act on.</param> /// <param name="fullName">Name of the full.</param> /// <param name="inherit">true to inherit.</param> /// <returns>The custom attribute by full name.</returns> public static object GetCustomAttributeByFullName(this MemberInfo @this, string fullName, bool inherit) { var attributes = @this.GetCustomAttributes(inherit).Where(x => x.GetType().FullName == fullName).ToArray(); if (attributes.Length == 0) { return null; } if (attributes.Length == 1) { return attributes[0]; } throw new Exception(string.Format("Ambiguous attribute. Multiple custom attributes of the same type found: {0} attributes found.", attributes.Length)); } }