14.6 Retrieving a Custom Attribute at Runtime

Retrieving attributes at runtime is done using reflection via one of System.Attribute's GetCustomAttribute or GetCustomAttributes overloads. Here is an example that uses and inspects the CrossRefAttribute using GetCustomAttribute:

// XRefTest.cs - apply and inspect a CrossRefAttribute
// Compile with: csc /r:XRef.dll XRefTest.cs
using System;
class Bar { }
[CrossRef(typeof(Bar), Description="Foos often hang around Bars")]
class Foo {
  static void Main( ) {
    // Retrieve the custom attribute from type Foo
    Attribute attr =
    // Display the attribute.
    if (attr != null) {
      CrossRefAttribute cr = (CrossRefAttribute)attr;

This is one of the few circumstances where the difference between custom attributes and pseudocustom attributes becomes apparent, since pseudocustom attributes can't be retrieved with GetCustomAttribute. Here is another example that uses reflection to determine which attributes are on a specific type:

using System;
[Serializable, Obsolete]
class Test {
  static void Main( ) {
    Type t = typeof(Test);
    object[ ] caarr = Attribute.GetCustomAttributes(t);
    Console.WriteLine("{0} has {1} custom attribute(s)",
                      t, caarr.Length);
    foreach (object ca in caarr)

Although the Test class of the preceding example has two attributes specified, the sample produces the following output:

Test has 1 custom attribute(s)

This demonstrates how the Serializable attribute (a pseudocustom attribute) isn't accessible via reflection, while the Obsolete attribute (a custom attribute) is.

    Part II: Programming with the .NET Framework
    Part IV: API Quick Reference