Accessing Metadata

We'll start our discussion of reflection by extracting metadata from the Author attributes we created in the previous example. We can read those attributes at runtime with the System.Reflection.MemberInfo class's GetCustomAttributes method, which works like this:

public abstract object[] GetCustomAttributes
(Type attributeType, bool inherit)

Here are the arguments you pass to this method:

  • attributeType The type of attributes you want.

  • inherit Specifies whether to search the inheritance chain to find attributes.

For example, to access the properties of the [Author] attributes we created in ch14_02.cs, we can create a MemberInfo object and call that object's GetCustomAttributes method to get an array of Author attribute ...

Get Microsoft® Visual C#® .NET 2003 Kick Start now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.