Object - GetCustomAttributes

An object extension method that gets custom attributes.

Try it

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

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

    Console.WriteLine(result1[0].Text);
    Console.WriteLine(result2[0].Text);
    Console.WriteLine(result3[0].Text);
    Console.WriteLine(result4[0].Text);
    Console.WriteLine(((MyTestAttribute) result5[0]).Text);
    Console.WriteLine(((MyTestAttribute) result6[0]).Text);
    Console.WriteLine(result7[0].Text);
    Console.WriteLine(result8[0].Text);
    Console.WriteLine(((MyTestAttribute) result9[0]).Text);
    Console.WriteLine(((MyTestAttribute) result10[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.Reflection;

public static partial class Extensions
{
    /// <summary>An object extension method that gets custom attributes.</summary>
    /// <param name="this">The @this to act on.</param>
    /// <returns>An array of object.</returns>
    public static Attribute[] GetCustomAttributes(this object @this)
    {
        var type = @this.GetType();

        return type.IsEnum ?
            type.GetField(@this.ToString()).GetCustomAttributes() :
            type.GetCustomAttributes();
    }

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

        return type.IsEnum ?
            type.GetField(@this.ToString()).GetCustomAttributes(inherit) :
            type.GetCustomAttributes(inherit);
    }

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

        return (T[]) (type.IsEnum ?
            type.GetField(@this.ToString()).GetCustomAttributes(typeof (T)) :
            type.GetCustomAttributes(typeof (T)));
    }

    /// <summary>
    ///     An object extension method that gets custom attributes.
    /// </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>An array of object.</returns>
    public static T[] GetCustomAttributes<T>(this object @this, bool inherit) where T : Attribute
    {
        var type = @this.GetType();

        return (T[]) (type.IsEnum ?
            type.GetField(@this.ToString()).GetCustomAttributes(typeof (T), inherit) :
            type.GetCustomAttributes(typeof (T), inherit));
    }

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

    /// <summary>An object extension method that gets custom attributes.</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>An array of object.</returns>
    public static T[] GetCustomAttributes<T>(this MemberInfo @this, bool inherit) where T : Attribute
    {
        return (T[]) @this.GetCustomAttributes(typeof (T), inherit);
    }
}