Click here to Skip to main content
Click here to Skip to main content

Get CustomAttributes the easy way....

By , 16 Apr 2010
 
A few extension methods to help out.....
 
// Project: Salient.Reflection
// http://salient.codeplex.com

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
 
public static class AttributeHelpers
{
    /// <summary>
    /// Returns first non-inherited custom attribute of type T
    /// </summary>
    public static T GetCustomAttribute<T>(this ICustomAttributeProvider provider)
        where T : Attribute
    {
        return GetCustomAttribute<T>(provider, false);
    }
 
    /// <summary>
    /// Returns first custom attribute of type T in the inheritance chain
    /// </summary>
    public static T GetCustomAttribute<T>(this ICustomAttributeProvider provider, bool inherited)
        where T : Attribute
    {
        return provider.GetCustomAttributes<T>(inherited).FirstOrDefault();
    }
 
    /// <summary>
    /// Returns all non-inherited custom attributes of type T 
    /// </summary>
    public static List<T> GetCustomAttributes<T>(this ICustomAttributeProvider provider) 
        where T : Attribute
    {
        return GetCustomAttributes<T>(provider, false);
    }
 
    /// <summary>
    /// Returns all custom attributes of type T in the inheritance chain
    /// </summary>
    public static List<T> GetCustomAttributes<T>(this ICustomAttributeProvider provider, bool inherited)
        where T : Attribute
    {
        return provider.GetCustomAttributes(typeof (T), inherited).Cast<T>().ToList();
    }
}
 

 

Thanks to Wouter Ballet.

To tonyt: Regarding linq abuse, although I have factored out the linq expression you complained about, it was intentional to deal with a casting issue. I invite you to check the previous revision and make that expression work in the order you propose without a lot of unnecessary bloat. But thanks for the feedback.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Sky Sanders
Software Developer (Senior) Salient Solutions
United States United States
Member
My name is Sky Sanders and I am an end-to-end, front-to-back software solutions architect with more than 20 years experience in IT infrastructure and software development, the last 10 years being focused primarily on the Microsoft .NET platform.
 
My motto is 'I solve problems.' and I am currently available for hire.
 
I can be contacted at sky.sanders@gmail.com

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 16 Apr 2010
Article Copyright 2010 by Sky Sanders
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid