Object - GetCustomAttributesByFullName
An object extension method that gets custom attributes by full name.
public static void Main() { var thisClass = new MyTestClass(); var thisEnum = MyTestEnum.Test; //C# Extension Method: Object - GetCustomAttributesByFullName var result1 = thisClass.GetCustomAttributesByFullName("MyTestAttribute"); var result2 = thisClass.GetCustomAttributesByFullName("MyTestAttribute", false); var result3 = thisClass.GetType().GetCustomAttributesByFullName("MyTestAttribute"); var result4 = thisClass.GetType().GetCustomAttributesByFullName("MyTestAttribute", false); var result5 = thisEnum.GetCustomAttributesByFullName("MyTestAttribute"); var result6 = thisEnum.GetCustomAttributesByFullName("MyTestAttribute", false); Console.WriteLine(((MyTestAttribute) result1[0]).Text); Console.WriteLine(((MyTestAttribute) result2[0]).Text); Console.WriteLine(((MyTestAttribute) result3[0]).Text); Console.WriteLine(((MyTestAttribute) result4[0]).Text); Console.WriteLine(((MyTestAttribute) result5[0]).Text); Console.WriteLine(((MyTestAttribute) result6[0]).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>An object extension method that gets custom attributes by full name.</summary> /// <param name="this">The @this to act on.</param> /// <param name="fullName">Name of the full.</param> /// <returns>An array of attribute.</returns> public static Attribute[] GetCustomAttributesByFullName(this object @this, string fullName) { var type = @this.GetType(); return type.IsEnum ? type.GetField(@this.ToString()).GetCustomAttributes().Where(x => x.GetType().FullName == fullName).ToArray() : type.GetCustomAttributes().Where(x => x.GetType().FullName == fullName).ToArray(); } /// <summary>An object extension method that gets custom attributes by full name.</summary> /// <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>An array of attribute.</returns> public static object[] GetCustomAttributesByFullName(this object @this, string fullName, bool inherit) { var type = @this.GetType(); return 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(); } /// <summary>An object extension method that gets custom attributes by full name.</summary> /// <param name="this">The @this to act on.</param> /// <param name="fullName">Name of the full.</param> /// <returns>An array of attribute.</returns> public static Attribute[] GetCustomAttributesByFullName(this MemberInfo @this, string fullName) { return @this.GetCustomAttributes().Where(x => x.GetType().FullName == fullName).ToArray(); } /// <summary>An object extension method that gets custom attributes by full name.</summary> /// <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>An array of attribute.</returns> public static object[] GetCustomAttributesByFullName(this MemberInfo @this, string fullName, bool inherit) { return @this.GetCustomAttributes(inherit).Where(x => x.GetType().FullName == fullName).ToArray(); } }