Object - GetCustomAttribute

An object extension method that gets the first custom attribute.

Try it

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

    //C# Extension Method: Object - GetCustomAttribute
    var result1 = thisClass.GetCustomAttribute<MyTestAttribute>();
    var result2 = thisClass.GetCustomAttribute<MyTestAttribute>(false);
    var result3 = thisClass.GetType().GetCustomAttribute<MyTestAttribute>();
    var result4 = thisClass.GetType().GetCustomAttribute<MyTestAttribute>(false);
    var result5 = thisClass.GetCustomAttribute(typeof(MyTestAttribute));
    var result6 = thisClass.GetCustomAttribute(typeof(MyTestAttribute), false);
    var result7 = thisClass.GetType().GetCustomAttribute(typeof(MyTestAttribute));
    var result8 = thisClass.GetType().GetCustomAttribute(typeof(MyTestAttribute), false);
    var result9 = thisEnum.GetCustomAttribute<MyTestAttribute>();
    var result10 = thisEnum.GetCustomAttribute<MyTestAttribute>(false);
    var result11 = thisEnum.GetCustomAttribute(typeof(MyTestAttribute));
    var result12 = thisEnum.GetCustomAttribute(typeof(MyTestAttribute), false);

    Console.WriteLine(result1.Text);
    Console.WriteLine(result2.Text);
    Console.WriteLine(result3.Text);
    Console.WriteLine(result4.Text);
    Console.WriteLine(((MyTestAttribute)result5).Text);
    Console.WriteLine(((MyTestAttribute)result6).Text);
    Console.WriteLine(((MyTestAttribute)result7).Text);
    Console.WriteLine(((MyTestAttribute)result8).Text);
    Console.WriteLine(result9.Text);
    Console.WriteLine(result10.Text);
    Console.WriteLine(((MyTestAttribute)result11).Text);
    Console.WriteLine(((MyTestAttribute)result12).Text);
}
}

[AttributeUsage(AttributeTargets.All)]
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.Reflection;

public static partial class Extensions
{
    /// <summary>An object extension method that gets the first custom attribute.</summary>
    /// <param name="this">The @this to act on.</param>
    /// <param name="attribute">The attribute.</param>
    /// <returns>The custom attribute.</returns>
    public static object GetCustomAttribute(this object @this, Type attribute)
    {
        var type = @this.GetType();

        return type.IsEnum ?
            Attribute.GetCustomAttribute(type.GetField(@this.ToString()), attribute) :
            Attribute.GetCustomAttribute(type, attribute);
    }

    /// <summary>
    ///     An object extension method that gets the first custom attribute.
    /// </summary>
    /// <param name="this">The @this to act on.</param>
    /// <param name="attribute">The attribute.</param>
    /// <param name="inherit">true to inherit.</param>
    /// <returns>The custom attribute.</returns>
    public static object GetCustomAttribute(this object @this, Type attribute, bool inherit)
    {
        var type = @this.GetType();

        return type.IsEnum ?
            Attribute.GetCustomAttribute(type.GetField(@this.ToString()), attribute, inherit) :
            Attribute.GetCustomAttribute(type, attribute, inherit);
    }

    /// <summary>An object extension method that gets custom attribute.</summary>
    /// <typeparam name="T">Generic type parameter.</typeparam>
    /// <param name="this">The @this to act on.</param>
    /// <returns>The custom attribute.</returns>
    public static T GetCustomAttribute<T>(this object @this) where T : Attribute
    {
        var type = @this.GetType();

        return (T) (type.IsEnum ?
            Attribute.GetCustomAttribute(type.GetField(@this.ToString()), typeof (T)) :
            Attribute.GetCustomAttribute(type, typeof (T)));
    }

    /// <summary>
    ///     An object extension method that gets custom attribute.
    /// </summary>
    /// <typeparam name="T">Generic type parameter.</typeparam>
    /// <param name="this">The @this to act on.</param>
    /// <param name="inherit">true to inherit.</param>
    /// <returns>The custom attribute.</returns>
    public static T GetCustomAttribute<T>(this object @this, bool inherit) where T : Attribute
    {
        var type = @this.GetType();

        return (T) (type.IsEnum ?
            Attribute.GetCustomAttribute(type.GetField(@this.ToString()), typeof (T), inherit) :
            Attribute.GetCustomAttribute(type, typeof (T), inherit));
    }

    /// <summary>An object extension method that gets custom attribute.</summary>
    /// <typeparam name="T">Generic type parameter.</typeparam>
    /// <param name="this">The @this to act on.</param>
    /// <returns>The custom attribute.</returns>
    public static T GetCustomAttribute<T>(this MemberInfo @this) where T : Attribute
    {
        return (T) Attribute.GetCustomAttribute(@this, typeof (T));
    }

    /// <summary>An object extension method that gets custom attribute.</summary>
    /// <typeparam name="T">Generic type parameter.</typeparam>
    /// <param name="this">The @this to act on.</param>
    /// <param name="inherit">true to inherit.</param>
    /// <returns>The custom attribute.</returns>
    public static T GetCustomAttribute<T>(this MemberInfo @this, bool inherit) where T : Attribute
    {
        return (T) Attribute.GetCustomAttribute(@this, typeof (T), inherit);
    }
}