Reflection – get attribute name and value on property

I have a class, lets call it Book with a property called Name. With that property, I have an attribute associated with it.

public class Book
{
    [Author("AuthorName")]
    public string Name
    {
        get; private set; 
    }
}

In my main method, I’m using reflection and wish to get key value pair of each attribute for each property. So in this example, I’d expect to see “Author” for attribute name and “AuthorName” for the attribute value.

Question: How do I get the attribute name and value on my properties using Reflection?

16 Answers
16

Leave a Comment