Concept Overview
Reflection in C# allows you to:
Inspect types, methods, properties, and assemblies at runtime.
Dynamically create objects or invoke methods without knowing their type at compile time.
In simple terms, Reflection gives your program the ability to look at itself and manipulate code dynamically.
Real-Time Scenario
Imagine you have an ASP.NET WebForms admin page that needs to:
Load a class dynamically (e.g., Employee class).
Display its properties and methods in a web page (for audit, documentation, or testing).
This helps when:
Step-by-Step Example
1. Create an Example Class (Employee.cs)
namespace WebApp.Models
{
public class Employee
{
public int EmpID { get; set; }
public string Name { get; set; }
public double Salary { get; set; }
public Employee() { }
public Employee(int id, string name, double salary)
{
EmpID = id;
Name = name;
Salary = salary;
}
public string GetEmployeeDetails()
{
return $"ID: {EmpID}, Name: {Name}, Salary: {Salary:C}";
}
}
}
2. Web Form: ReflectionDemo.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ReflectionDemo.aspx.cs" Inherits="WebApp.ReflectionDemo" %>
<!DOCTYPE html>
<html>
<head>
<title>Reflection in C# - ASP.NET WebForms Example</title>
</head>
<body>
<form id="form1" runat="server">
<h2>Reflection Example - Inspecting a Class Dynamically</h2>
<asp:Button ID="btnInspect" runat="server" Text="Inspect Employee Class" OnClick="btnInspect_Click" />
<br /><br />
<asp:Label ID="lblResult" runat="server" Text=""></asp:Label>
</form>
</body>
</html>
3. Code Behind: ReflectionDemo.aspx.cs
using System;
using System.Reflection;
using System.Text;
using WebApp.Models;
namespace WebApp
{
public partial class ReflectionDemo : System.Web.UI.Page
{
protected void btnInspect_Click(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
// 1. Get the Type of the Employee class
Type empType = typeof(Employee);
sb.Append($"<b>Class Name:</b> {empType.FullName}<br/>");
sb.Append("<hr/>");
// 2. Get Properties
sb.Append("<b>Properties:</b><br/>");
foreach (PropertyInfo prop in empType.GetProperties())
{
sb.Append($"{prop.PropertyType.Name} {prop.Name}<br/>");
}
sb.Append("<hr/>");
// 3. Get Methods
sb.Append("<b>Methods:</b><br/>");
foreach (MethodInfo method in empType.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly))
{
sb.Append($"{method.ReturnType.Name} {method.Name}()<br/>");
}
sb.Append("<hr/>");
// 4. Dynamically Create an Instance
object empObj = Activator.CreateInstance(empType);
// Set property values dynamically
PropertyInfo nameProp = empType.GetProperty("Name");
PropertyInfo idProp = empType.GetProperty("EmpID");
PropertyInfo salaryProp = empType.GetProperty("Salary");
idProp.SetValue(empObj, 101);
nameProp.SetValue(empObj, "Sandhiya V");
salaryProp.SetValue(empObj, 55000.50);
// 5. Invoke a Method Dynamically
MethodInfo detailsMethod = empType.GetMethod("GetEmployeeDetails");
string details = (string)detailsMethod.Invoke(empObj, null);
sb.Append("<b>Dynamically Created Employee Object:</b><br/>");
sb.Append(details + "<br/>");
lblResult.Text = sb.ToString();
}
}
}
Output
When you click “Inspect Employee Class”, you’ll see:
Class Name: WebApp.Models.Employee
--------------------------------
Properties:
Int32 EmpID
String Name
Double Salary
--------------------------------
Methods:
String GetEmployeeDetails()
--------------------------------
Dynamically Created Employee Object:
ID: 101, Name: Sandhiya V, Salary: ₹55,000.50
Real-Time Use Cases of Reflection in WebForms
| Use Case | Description |
|---|
| Dynamic Form Generation | Auto-create forms by reading model properties at runtime. |
| Custom Validation Frameworks | Dynamically read attributes like [Required] or [EmailAddress]. |
| Plugin/Module Loading | Load external DLLs dynamically (used in CMS or e-commerce admin panels). |
| Object Mapping / ORM | Used in Entity Framework & Newtonsoft.Json internally. |
| Audit/Documentation Tools | Inspect and list all available methods & classes dynamically. |
Key Points
Type class gives access to all metadata of a class.
PropertyInfo and MethodInfo let you read or modify members.
Activator.CreateInstance() creates an object dynamically.
Reflection is powerful but can be slower — use it carefully in performance-critical scenarios.