Object - GetCustomAttributeByName

An object extension method that gets custom attribute by name.

Try it

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

    //C# Extension Method: Object - GetCustomAttributeByName		
	var result1 = thisClass.GetCustomAttributeByName("MyTestAttribute");
    var result2 = thisClass.GetCustomAttributeByName("MyTestAttribute", false);
    var result3 = thisClass.GetType().GetCustomAttributeByName("MyTestAttribute");
    var result4 = thisClass.GetType().GetCustomAttributeByName("MyTestAttribute", false);
    var result5 = thisEnum.GetCustomAttributeByName("MyTestAttribute");
    var result6 = thisEnum.GetCustomAttributeByName("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>An object extension method that gets custom attribute by name.</summary>
    /// <exception cref="Exception">Thrown when an exception error condition occurs.</exception>
    /// <param name="this">The @this to act on.</param>
    /// <param name="name">The name.</param>
    /// <returns>The custom attribute by name.</returns>
    public static object GetCustomAttributeByName(this object @this, string name)
    {
        var type = @this.GetType();

        var attributes = type.IsEnum ?
            type.GetField(@this.ToString()).GetCustomAttributes().Where(x => x.GetType().Name == name).ToArray() :
            type.GetCustomAttributes().Where(x => x.GetType().Name == name).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>An object extension method that gets custom attribute by name.</summary>
    /// <exception cref="Exception">Thrown when an exception error condition occurs.</exception>
    /// <param name="this">The @this to act on.</param>
    /// <param name="name">The name.</param>
    /// <param name="inherit">true to inherit.</param>
    /// <returns>The custom attribute by name.</returns>
    public static object GetCustomAttributeByName(this object @this, string name, bool inherit)
    {
        var type = @this.GetType();

        var attributes = 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();

        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>An object extension method that gets custom attribute by name.</summary>
    /// <exception cref="Exception">Thrown when an exception error condition occurs.</exception>
    /// <param name="this">The @this to act on.</param>
    /// <param name="name">The name.</param>
    /// <returns>The custom attribute by name.</returns>
    public static object GetCustomAttributeByName(this MemberInfo @this, string name)
    {
        var attributes = @this.GetCustomAttributes().Where(x => x.GetType().Name == name).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>An object extension method that gets custom attribute by name.</summary>
    /// <exception cref="Exception">Thrown when an exception error condition occurs.</exception>
    /// <param name="this">The @this to act on.</param>
    /// <param name="name">The name.</param>
    /// <param name="inherit">true to inherit.</param>
    /// <returns>The custom attribute by name.</returns>
    public static object GetCustomAttributeByName(this MemberInfo @this, string name, bool inherit)
    {
        var attributes = @this.GetCustomAttributes(inherit).Where(x => x.GetType().Name == name).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));
    }
}