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:

PropertyUses
AssemblyTo return the Assembly for this type.
AssemblyQualifiedNameTo return the Assembly qualified name for this type.
AttributesTo return the Attributes associated with the type.
BaseTypeTo return the base or parent type.
FullNameTo return the fully qualified name of the type.
IsAbstractTo determine if the type is Abstract.
IsArrayTo determine if the type is Array.
IsClassTo determine if the type is Class.
IsEnumTo determine if the type is Enum.
IsInterfaceTo determine if the type is Interface.
IsNestedTo determine if the type is Nested.
IsPrimitiveTo determine if the type is Primitive.
IsPointerTo determine if the type is Pointer.
IsNotPublicTo determine if the type is not Public.
IsPublicTo determine if the type is Public.
IsSealedTo determine if the type is Sealed.
IsSerializableTo determine if the type is Serializable.
MemberTypeTo determine if the type is Member type of Nested type.
ModuleTo return the module of the type.
NameTo return the name of the type.
NamespaceTo return the namespace of the type.

C# Type Methods:

The important methods of Type class are listed below:

MethodUses
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.

Content Protection by DMCA.com
Please Share