Introduction
The reflection concept in C# is the ability to inspect the metadata of an assembly at runtime. In other words, assembly content is described by looking at the assembly metadata at runtime by loading a library in memory and reading its attributes.&nIn this article, I'll try to explain reflection concepts and how can it be useful in applications.
When it comes to using reflection in practice scenarios, it is very confusing for the developers to use the reflection concept properly and get benefit from this beautiful feature. However, reflection is not recommended in obvious conditions. It may slow the performance of an application.
What is Reflection in C#?
Reflection is the ability of a managed code to read its own metadata for the purpose of finding assemblies, modules, and type information at runtime. The classes that give access to the metadata of a running program are in System. Reflection.
System. Reflection namespace defines the following types to analyze the module's metadata of an assembly: Assembly, Module, Enum, ParameterInfo, MemberInfo, Type, MethodInfo, ConstructorInfo, FieldInfo, EventInfo, and PropertyInfo.
Assembly Metadata
y is a logical DLL or EXE, which is described by the manifest, the detailed description (metadata) of an assembly. The .NET compiler produces a portable executable PE file for CLR with the extensions of .exe or .dll. This PE file is mainly comprised of metadata and IL (Intermediate Language).
Explanation
When a compiler builds a binary (dll or exe), the code gets compiled into the Intermediate Language (IL) and then packaged in an assembly. An assembly contains metadata which has information about the type of classes and their definitions, including their members, fields, constructors, properties, methods, functions,s, etc.
By using reflection, an assembly can be inspected.
Type _type = Type.GetType("ReflectionConcept.Employee");
// Or
Type _type = typeof(Employee);

Sample Code
using System;
using System.Reflection;
namespace ReflectionConcept
{
public class Employee
{
public int empID { get; set; }
public string empName { get; set; }
public float Salary { get; set; }
public Employee()
{
this.empID = -1;
this.empName = string.Empty;
this.Salary = 0;
}
public Employee(int id, string name, float salary)
{
this.empID = id;
this.empName = name;
this.Salary = salary;
}
public void displayName()
{
Console.WriteLine("Name: " + this.empName);
}
public void printSalary()
{
Console.WriteLine("Salary: " + this.Salary);
}
}
class Program
{
static void Main(string[] args)
{
Type _type = Type.GetType("ReflectionConcept.Employee");
// Type _type = typeof(Employee);
Console.WriteLine(_type.FullName);
Console.WriteLine(_type.Namespace);
Console.WriteLine(_type.Name);
PropertyInfo[] info = _type.GetProperties();
foreach (PropertyInfo propinfo in info)
{
Console.WriteLine(propinfo.Name);
}
MethodInfo[] methods = _
Uses of Reflections
Refection is heavily used by developer IDEs or UI designers such as Visual Studio. The Property window of a .NET application uses reflection to show a list of properties.
still confused; alright, let me take an example of a Web application.


Brian DumasPosted Jun 18, 2019, 11:34 AM
Hello Mr Singh, Can you tell me is it possible to use reflection into a running C# executable? I can load an assembly to reflect into, but that just runs another copy of the running executable. Thanks, Brian
Ayub MuhammadPosted Apr 5, 2019, 7:37 AM
Hi Sir, how can i get access to private property of the class with reflection?
Narasimharao VunnamPosted Nov 3, 2018, 2:19 AM
This code is not Working. Please Check it Once