---
title: "Reflection in C#"  
description: "In this article, I’m trying to explain the concept of reflection."  
author: "Sumit Kesarwani"  
published: 2013-05-14  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/1297/reflection-in-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 3 minutes  

---

# Reflection in C#

In this article, I’m trying to explain the [concept of reflection](https://www.mindstick.com/forum/158580/discuss-the-concept-of-reflection-in-the-dot-net-framework-and-its-practical-applications).\

Reflection allows a [C# program](https://www.mindstick.com/forum/157401/c-sharp-program-to-find-all-the-divisible-numbers-by-7-which-are-less-than-100) 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](https://www.mindstick.com/articles/28/namespace-in-dot-net), 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](https://www.mindstick.com/articles/87/passing-parameters-in-c-sharp) 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](https://www.mindstick.com/articles/12378/visual-studio-for-mac-is-out-of-beta-preview-now-officially-available) [Command Prompt](https://answers.mindstick.com/qa/51716/how-to-make-bootable-pendrive-using-cmd-windows-command-prompt) and type the following command:

![Reflection in C#](https://www.mindstick.com/mindstickarticle/823e6e34-78a4-4646-9055-3a692327622e/images/33092134-ee8a-4837-b6b7-9ed6a2a12c27.png)

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#](https://www.mindstick.com/mindstickarticle/823e6e34-78a4-4646-9055-3a692327622e/images/828bc9da-b8c8-43cd-84a5-230f6b4b3907.png)

csc ReflectTypes.cs

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

##### Step 5:-

Run the ReflectTypes.exe with one [command line](https://www.mindstick.com/forum/10/is-there-a-method-to-get-command-line-arguments-in-vb-dot-net) argument

![Reflection in C#](https://www.mindstick.com/mindstickarticle/823e6e34-78a4-4646-9055-3a692327622e/images/3dc2e0f5-7789-455a-8bf2-27fa97295404.png)

ReflectTypes testingReflection.dll

Execute this command and you will get the desired output.

---

Original Source: https://www.mindstick.com/articles/1297/reflection-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
