C Sharp Reflection

C# Reflection

A process to get metadata of a type at runtime is called reflection in C#. The required classes for reflection are found in the System.Reflection namespace. Some of these classes are:

  • Type
  • MemberInfo
  • ConstructorInfo
  • MethodInfo
  • FieldInfo
  • PropertyInfo
  • TypeInfo
  • EventInfo
  • Module
  • Assembly
  • AssemblyName
  • Pointer etc.

The classes to emit metadata are found in the System.Reflection.Emit namespace.

C# Type class:

The type declarations for class types, interface types, enumeration types, array types, value types etc. is defined by the C# Type class. Being found in the System namespace, the C# Type class inherits the System.Reflection.MemberInfo class.

C# Type Properties:

The important properties of Type class are listed below:

Property Uses
Assembly To return the Assembly for this type.
AssemblyQualifiedName To return the Assembly qualified name for this type.
Attributes To return the Attributes associated with the type.
BaseType To return the base or parent type.
FullName To return the fully qualified name of the type.
IsAbstract To determine if the type is Abstract.
IsArray To determine if the type is Array.
IsClass To determine if the type is Class.
IsEnum To determine if the type is Enum.
IsInterface To determine if the type is Interface.
IsNested To determine if the type is Nested.
IsPrimitive To determine if the type is Primitive.
IsPointer To determine if the type is Pointer.
IsNotPublic To determine if the type is not Public.
IsPublic To determine if the type is Public.
IsSealed To determine if the type is Sealed.
IsSerializable To determine if the type is Serializable.
MemberType To determine if the type is Member type of Nested type.
Module To return the module of the type.
Name To return the name of the type.
Namespace To return the namespace of the type.

C# Type Methods:

The important methods of Type class are listed below:

Method Uses
GetConstructors() To get all the public constructors for the Type.
GetConstructors(BindingFlags) To get all the constructors for the Type with specified BindingFlags.
GetFields() To get all the public fields for the Type.
GetFields(BindingFlags) To get all the public constructors for the Type with specified BindingFlags.
GetMembers() To get all the public members for the Type.
GetMembers(BindingFlags) To get all the members for the Type with specified BindingFlags.
GetMethods() To get all the public methods for the Type.
GetMethods(BindingFlags) To get all the methods for the Type with specified BindingFlags.
GetProperties() To get all the public properties for the Type.
GetProperties(BindingFlags) To get all the properties for the Type with specified BindingFlags.
GetType() To get the current Type.
GetType(String) To get the Type for the given name.

Example:

using System;  
public class Example  
{  
   public static void Main()  
    {  
        int num = 100;  
        Type x = num.GetType();  
        Console.WriteLine(x);  
    }  
}

Output:

Explanation:

In the above example, we are displaying the use and behavior of the Get Type method of the Type class.

Example:

using System;  
using System.Reflection;  
public class Example  
{  
   public static void Main()  
    {  
        Type x = typeof(System.String);  
        Console.WriteLine(x.Assembly);   
   }  
}

Output:

Explanation:

In the above example, we are displaying the use and behavior of the Get Assembly method of the Type class.

Example:

using System;  
using System.Reflection;  
public class Example  
{  
    public static void Main()  
    {  
        Type x = typeof(System.String);  
        Console.WriteLine(x.FullName);  
        Console.WriteLine(x.BaseType);  
        Console.WriteLine(x.IsClass);  
        Console.WriteLine(x.IsEnum);  
        Console.WriteLine(x.IsInterface);  
    }  
}

Output:

Explanation:

In the above example, we are using the different properties of the Type class to print the type information.

Example:

using System;  
using System.Reflection;  
public class Example  
{  
    public static void Main()  
    {  
        Type x = typeof(System.String);  
 
        Console.WriteLine("{0} type Constructors:", x);  
        ConstructorInfo[] info = x.GetConstructors(BindingFlags.Public | BindingFlags.Instance);  
        foreach (ConstructorInfo i in info)  
        {  
            Console.WriteLine(i);  
        }  
    }  
}

Output:

Explanation:

In the above example, we are using the GetConstructors(BindingFlags) method to get all the constructors for the Type with specified BindingFlags.

Example:

using System;  
using System.Reflection;  
public class Example  
{  
    public static void Main()  
    {  
        Type x = typeof(System.String);  
 
        Console.WriteLine("{0} type Methods:", x);  
        MethodInfo[] info = x.GetMethods(BindingFlags.Public | BindingFlags.Instance);  
        foreach (MethodInfo i in info)  
        {  
            Console.WriteLine(i);  
        }  
    }  
}

Output:

Explanation:

In the above example, we are using the GetMethods(BindingFlags) method to get all the methods for the Type with specified BindingFlags.

Example:

using System;  
using System.Reflection;  
public class Example  
{  
    public static void Main()  
    {  
        Type x = typeof(System.String);  
 
        Console.WriteLine("{0} type Fields:", x);  
        FieldInfo[] info = x.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic);  
        foreach (FieldInfo i in info)  
        {  
            Console.WriteLine(i);  
        }  
    }  
}

Output:

Explanation:

In the above example, we are using the GetFields(BindingFlags) method to get all the public constructors for the Type with specified BindingFlags.