O'Reilly logo

C# Primer: A Practical Approach by Stanley B. Lippman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

8.8. Runtime Discovery of Attributes Using Reflection

Now that we've decorated our classes with custom attributes, we'll want to access them during the execution of our program. There are several ways to do that. For example, we can invoke the static GetCustomAttributes() method of the Attribute class, passing it a Type object:

static public void retrieveClass1( object obj )
{
    Type tp = obj.GetType();

    Attribute [] attrs =
                 Attribute.GetCustomAttributes( tp );

An Attribute array holding an instance of each custom attribute retrieved is returned. If no custom attributes are present, an empty array is returned. Note that intrinsic attributes are not retrieved. For example, the intrinsic Serializable attribute of our class is not returned.

The instance ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required