Use the GetCustomAttributes method : Attributes Reflection « Attribute « C# / CSharp Tutorial






using System;

public class TrueFalseAttribute : Attribute
{
  bool bWritten;

  public bool Written()
  {
    return bWritten;
  }

  public TrueFalseAttribute(bool Written)
  {
    bWritten = Written;
  }
}

public class StringAttribute : Attribute
{
  string sStage;

  public string Stage()
  {
    return sStage;
  }

  public StringAttribute(string Stage)
  {
    sStage = Stage;
  }
}

[TrueFalseAttribute(true)]
[StringAttribute("Coding")]
public class Class1
{
}

class MainClass
{
  public static void Main() 
  {
    Console.WriteLine("Class1 attributes: ");object[] aAttributes = Attribute.GetCustomAttributes(typeof(Class1));
    foreach (object attr in aAttributes)
    {
      Console.WriteLine(attr);
    }
  }
}
Class1 attributes:
StringAttribute
TrueFalseAttribute








10.5.Attributes Reflection
10.5.1.Reflecting on Attributes
10.5.2.Retrieve Attribute by using reflection
10.5.3.Use Reflection to get the Attribute
10.5.4.Use the GetCustomAttributes method
10.5.5.Load class method by Attribute