C# - Reflection

Reflection objects are used for obtaining type information at runtime. The classes that give access to the metadata of a running program are in the System.Reflection namespace.

The System.Reflection namespace contains classes that allow you to obtain information about the application and to dynamically add types, values, and objects to the application.

Applications of Reflection

Reflection has the following applications −

  • It allows view attribute information at runtime.

  • It allows examining various types in an assembly and instantiate these types.

  • It allows late binding to methods and properties

  • It allows creating new types at runtime and then performs some tasks using those types.

Viewing Metadata

We have mentioned in the preceding chapter that using reflection you can view the attribute information.

The MemberInfo object of the System.Reflection class needs to be initialized for discovering the attributes associated with a class. To do this, you define an object of the target class, as −

System.Reflection.MemberInfo info = typeof(MyClass);

The following program demonstrates this −

using System;

public class HelpAttribute : System.Attribute {
   public readonly string Url;
   public string Topic   // Topic is a named parameter {
      get {
         return topic;
      set {
         topic = value;
   public HelpAttribute(string url)   // url is a positional parameter {
      this.Url = url;
   private string topic;

[HelpAttribute("Information on the class MyClass")]
class MyClass {


namespace AttributeAppl {
   class Program {
      static void Main(string[] args) {
         System.Reflection.MemberInfo info = typeof(MyClass);
         object[] attributes = info.GetCustomAttributes(true);
         for (int i = 0; i < attributes.Length; i++) {

When it is compiled and run, it displays the name of the custom attributes attached to the class MyClass 



In this example, we use the DeBugInfo attribute created in the previous chapter and use reflection to read metadata in the Rectangle class.

 Live Demo

using System;
using System.Reflection;

namespace BugFixApplication {
   //a custom attribute BugFix to be assigned to a class and its members
      AttributeTargets.Class |
      AttributeTargets.Constructor |
      AttributeTargets.Field |
      AttributeTargets.Method |
      AllowMultiple = true)]

   public class DeBugInfo : System.Attribute {
      private int bugNo;
      private string developer;
      private string lastReview;
      public string message;
      public DeBugInfo(int bg, string dev, string d) {
         this.bugNo = bg;
         this.developer = dev;
         this.lastReview = d;
      public int BugNo {
         get {
            return bugNo;
      public string Developer {
         get {
            return developer;
      public string LastReview {
         get {
            return lastReview;
      public string Message {
         get {
            return message;
         set {
            message = value;
   [DeBugInfo(45, "Zara Ali", "12/8/2012", Message = "Return type mismatch")]
   [DeBugInfo(49, "Nuha Ali", "10/10/2012", Message = "Unused variable")]
   class Rectangle {
      //member variables
      protected double length;
      protected double width;
      public Rectangle(double l, double w) {
         length = l;
         width = w;
      [DeBugInfo(55, "Zara Ali", "19/10/2012", Message = "Return type mismatch")]
      public double GetArea() {
         return length * width;
      [DeBugInfo(56, "Zara Ali", "19/10/2012")]
      public void Display() {
         Console.WriteLine("Length: {0}", length);
         Console.WriteLine("Width: {0}", width);
         Console.WriteLine("Area: {0}", GetArea());
   }//end class Rectangle
   class ExecuteRectangle {
      static void Main(string[] args) {
         Rectangle r = new Rectangle(4.5, 7.5);
         Type type = typeof(Rectangle);
         //iterating through the attribtues of the Rectangle class
         foreach (Object