Object - GetCustomAttributesByName

An object extension method that gets custom attributes by name.

Try it

public static void Main()
{
    var thisClass = new MyTestClass();
    var thisEnum = MyTestEnum.Test;

    //C# Extension Method: Object - GetCustomAttributesByName				
	var result1 = thisClass.GetCustomAttributesByName("MyTestAttribute");
    var result2 = thisClass.GetCustomAttributesByName("MyTestAttribute", false);
    var result3 = thisClass.GetType().GetCustomAttributesByName("MyTestAttribute");
    var result4 = thisClass.GetType().GetCustomAttributesByName("MyTestAttribute", false);
    var result5 = thisEnum.GetCustomAttributesByName("MyTestAttribute");
    var result6 = thisEnum.GetCustomAttributesByName("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 name.</summary>
    /// <param name="this">The @this to act on.</param>
    /// <param name="name">The name.</param>
    /// <returns>An array of attribute.</returns>
    public static Attribute[] GetCustomAttributesByName(this object @this, string name)
    {
        var type = @this.GetType();

        return type.IsEnum ?
            type.GetField(@this.ToString()).GetCustomAttributes().Where(x => x.GetType().Name == name).ToArray() :
            type.GetCustomAttributes().Where(x => x.GetType().Name == name).ToArray();
    }

    /// <summary>An object extension method that gets custom attributes by name.</summary>
    /// <param name="this">The @this to act on.</param>
    /// <param name="name">The name.</param>
    /// <param name="inherit">true to inherit.</param>
    /// <returns>An array of attribute.</returns>
    public static object[] GetCustomAttributesByName(this object @this, string name, bool inherit)
    {
        var type = @this.GetType();

        return type.IsEnum ?
            type.GetField(@this.ToString()).GetCustomAttributes(inherit).Where(x => x.GetType().Name == name).ToArray() :
            type.GetCustomAttributes(inherit).Where(x => x.GetType().Name == name).ToArray();
    }

    /// <summary>An object extension method that gets custom attributes by name.</summary>
    /// <param name="this">The @this to act on.</param>
    /// <param name="name">The name.</param>
    /// <returns>An array of attribute.</returns>
    public static Attribute[] GetCustomAttributesByName(this MemberInfo @this, string name)
    {
        return @this.GetCustomAttributes().Where(x => x.GetType().Name == name).ToArray();
    }

    /// <summary>An object extension method that gets custom attributes by name.</summary>
    /// <param name="this">The @this to act on.</param>
    /// <param name="name">The name.</param>
    /// <param name="inherit">true to inherit.</param>
    /// <returns>An array of attribute.</returns>
    public static object[] GetCustomAttributesByName(this MemberInfo @this, string name, bool inherit)
    {
        return @this.GetCustomAttributes(inherit).Where(x => x.GetType().Name == name).ToArray();
    }
}