U2U Blog

for developers and other creative minds

TextBox

Extensions to get attribute information

Checking attributes on classes can be done by getting the type of the class and executing GetCustomAttributes() on it. You just need to iterate over the resulting object[] to find what you are interested in.

To simplify this a bit more, I wrote a couple of extension methods :

    static public class AttributeExtensions
    {
        static public T FindAttribute<T>(this object obj)
            where T : Attribute
        {
            return obj.GetType().GetCustomAttributes(true)
                .Where(attr => attr.GetType() == typeof(T))
                .Select(attr => attr as T).FirstOrDefault();
        }

        static public List<Attribute> GetAttributes(this object obj)
        {
            return obj.GetType().GetCustomAttributes(true).ConvertAllItems(c => c as Attribute).ToList();
        }

        static private IEnumerable<TOutput> ConvertAllItems<TInput, TOutput>(this IEnumerable<TInput> e, 
            Converter<TInput, TOutput> op)
        {
            foreach (TInput item in e)
            {
                yield return op(item);
            }
        }
    }
 

So, on a Person class like this

    [DataContract(Namespace="http://www.u2u.be/demo/entities")]
    public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }

    Person p = new Person() { Name = "John Doe", Age = 77 } ;

You can now find the DataContractAttribute by:

    DataContractAttribute attribute = p.FindAttribute<DataContractAttribute>();
    Console.WriteLine(attribute != null ? attribute.Namespace : "Attribute not found");
 

Or, to retrieve the list of attributes:

    List<Attribute> lst = p.GetAttributes();

Comments (24) -

  • internet blog

    6/28/2015 6:36:10 AM | Reply

    Hello! I just would like to give a huge thumbs up for the great info you have here on this post. I will be coming back to your blog for more soon.

  • m88 indonesia

    7/1/2015 1:14:39 PM | Reply

    After study a few of the blog posts on your website now, and I truly like your way of blogging. I bookmarked it to my bookmark website list and will be checking back soon. Pls check out my web site as well and let me know what you think.

  • web directory

    8/12/2015 4:59:12 PM | Reply

    I am often to blogging and i really appreciate your content. The article has really peaks my interest. I am going to bookmark your site and keep checking for new information.

  • seo directory

    9/6/2015 3:25:43 PM | Reply

    Can I just say what a relief to find someone who actually knows what theyre talking about on the internet. You definitely know how to bring an issue to light and make it important. More people need to read this and understand this side of the story. I cant believe youre not more popular because you definitely have the gift.

  • canbar büyüsü

    9/7/2015 6:23:39 PM | Reply

    Aw, this was a really nice post. In idea I would like to put in writing like this additionally – taking time and actual effort to make a very good article… but what can I say… I procrastinate alot and by no means seem to get something done.

  • COMINT

    9/13/2015 12:04:15 PM | Reply

    Would you be interested in exchanging links?

  • avrupabetin

    9/16/2015 9:55:23 AM | Reply

    I was very pleased to find this web-site.I wanted to thanks for your time for this wonderful read!! I definitely enjoying every little bit of it and I have you bookmarked to check out new stuff you blog post.

  • horror

    9/26/2015 10:59:23 PM | Reply

    There is noticeably a bundle to know about this. I assume you made certain nice points in features also.

  • web links

    9/30/2015 10:08:00 AM | Reply

    WONDERFUL Post.thanks for share..more wait .. …

  • free index

    10/16/2015 4:48:48 PM | Reply

    It’s hard to find knowledgeable people on this topic, but you sound like you know what you’re talking about! Thanks

  • immigration lawyer Camden

    10/21/2015 10:03:30 AM | Reply

    After study a few of the blog posts on your website now, and I truly like your way of blogging. I bookmarked it to my bookmark website list and will be checking back soon. Pls check out my web site as well and let me know what you think.

  • internet resources

    11/3/2015 10:10:13 AM | Reply

    Can I just say what a relief to find someone who actually knows what theyre talking about on the internet. You definitely know how to bring an issue to light and make it important. More people need to read this and understand this side of the story. I cant believe youre not more popular because you definitely have the gift.

  • paracaidismo madrid

    11/4/2015 8:57:38 AM | Reply

    This really answered my problem, thank you!

  • Aliso Niguel Eye Doctor

    11/6/2015 1:55:03 PM | Reply

    WONDERFUL Post.thanks for share..more wait .. …

  • sick

    11/24/2015 6:58:50 AM | Reply

    Nice post. I learn something more challenging on different blogs everyday. It will always be stimulating to read content from other writers and practice a little something from their store. I’d prefer to use some with the content on my blog whether you don’t mind. Natually I’ll give you a link on your web blog. Thanks for sharing.

  • Tall Tee

    12/7/2015 11:57:03 AM | Reply

    Spot on with this write-up, I truly think this website needs much more consideration. I’ll probably be again to read much more, thanks for that info.

  • iPhone Ad Blocker

    12/11/2015 2:41:41 PM | Reply

    I discovered your blog site on google and check a few of your early posts. Continue to keep up the very good operate. I just additional up your RSS feed to my MSN News Reader. Seeking forward to reading more from you later on!…

  • Instagram Followers

    12/12/2015 7:02:21 PM | Reply

    It’s hard to find knowledgeable people on this topic, but you sound like you know what you’re talking about! Thanks

  • foolish

    12/23/2015 2:18:17 AM | Reply

    Youre so cool! I dont suppose Ive read anything like this before. So nice to find somebody with some original thoughts on this subject. realy thank you for starting this up. this website is something that is needed on the web, someone with a little originality. useful job for bringing something new to the internet!

  • C &amp; R Medical

    1/13/2016 8:52:04 AM | Reply

    Hello! I just would like to give a huge thumbs up for the great info you have here on this post. I will be coming back to your blog for more soon.

Loading

TextBox