articles

Home / DeveloperSection / Articles / Reflection in C#

Reflection in C#

Sumit Kesarwani 3958 14-May-2013

In this article, I’m trying to explain the concept of reflection.

Reflection allows a C# program to inspect and manipulate itself. It can be used to effectively find all the types in an assembly and dynamically invoke methods in an assembly. It can at times even be used to emit intermediate language code on the fly so that the generated code can be executed directly.

Reflection is also used to obtain information about a class and its members. Reflection can be used to manipulate other objects on the .Net platform.

Reflection uses the System.Reflection namespace, with the type class to identity the Type of the class being reflected, and fields of a struct or enum represented by the the FieldInfo class, Members of the reflected class represented by the MemberInfo class. Methods of a reflected class represented by the MethodInfo class, and parameters to Methods represented by the ParameterInfo class.

Reflection can be used in three different ways:
  • Obtaining Class and Type Information from an assembly.
  • Obtaining Member Information from a Class.
  • Dynamically Invoking Methods from Classes in an Assembly.
Example
Step:-

Type the below code in notepad and save as testingReflection.cs


using System;
namespace TestingReflectionConsoleApplication1
{
    class testingReflection
    {
        testingReflection()
        {
            Console.WriteLine("Invoking Program Class");
        }
        public void execute(string data)
        {
            Console.WriteLine(data);
        }
        public void compute(int p1, int p2)
        {
            Console.WriteLine(p1*p2);
        }
    }
}

 Step 2:-

Open the Visual Studio Command Prompt and type the following command:

Reflection in C#

csc /t:library testingReflection.cs

This command create a dll file named testingRelection.dll

Step 3:-

Now open again notepad, type the following lines and save as ReflectTypes.cs in the same folder where you have saved testingReflection.cs file.

using System;
using System.Reflection;

namespace ReflectionConsoleApplication1
{
    class ReflectTypes
    {
        static void Main(string[] args)
        {
            Assembly assembly = Assembly.LoadFrom(args[0]);
            Type[] typearray = assembly.GetTypes();
            foreach (Type type in typearray)
            {
                Console.WriteLine("Type Name : " + type.FullName);
                Console.WriteLine("Type Namespace : " + type.Namespace);
            }
        }
    }
}

 Step 4:-

Type the following command in Visual Studio Command Prompt

Reflection in C#

csc ReflectTypes.cs

This command compile the file RelectTypes.cs and create ReflectTypes.exe

Step 5:-

Run the ReflectTypes.exe with one command line argument

Reflection in C#

ReflectTypes testingReflection.dll

Execute this command and you will get the desired output.


c# c# 
Updated 07-Sep-2019

Leave Comment

Comments

Liked By