What is Reflection in C#?
Last Updated :
19 Jul, 2024
Reflection is the process of describing the metadata of types, methods and fields in a code. The namespace System.Reflection enables you to obtain data about the loaded assemblies, the elements within them like classes, methods and value types. Some of the commonly used classes of System.Reflection are:
Class | Description |
---|
Assembly | describes an assembly which is a reusable, versionable, and self-describing building block of a common language runtime application |
AssemblyName | Identifies an assembly with a unique name |
ConstructorInfo | Describes a class constructor and gives access to the metadata |
MethodInfo | Describes the class method and gives access to its metadata |
ParameterInfo | Describes the parameters of a method and gives access to its metadata |
EventInfo | Describes the event info and gives accessto its metadata |
PropertyInfo | Discovers the attributes of a property and provides access to property metadata |
MemberInfo | Obtains information about the attributes of a member and provides access to member metadata |
Note:
There are numerous other classes, the above table gives info about only the commonly used. Let us now look at an example to depict how reflection works in C#.
Example 1:
In the code given below, we load the type t as a string using the typeof method. Then we apply reflection on t to find any information about string class, like its name, fullname, namespace, and basetype.
csharp
// C# program to illustrate
// the use of Reflection
using System;
using System.Reflection;
namespace Reflection_Demo {
class Program {
// Main Method
static void Main(string[] args)
{
// Initialise t as typeof string
Type t = typeof(string);
// Use Reflection to find about
// any sort of data related to t
Console.WriteLine("Name : {0}", t.Name);
Console.WriteLine("Full Name : {0}", t.FullName);
Console.WriteLine("Namespace : {0}", t.Namespace);
Console.WriteLine("Base Type : {0}", t.BaseType);
}
}
}
Output:
Name : String
Full Name : System.String
Namespace : System
Base Type : System.Object
Example 2:
In this code, we use reflection to show all the metadata related to the program which includes classes, methods of these classes and the parameters associated with these parameters.
csharp
// C# program to illustrate
// the use of Reflection
using System;
using System.Reflection;
namespace Reflection_Metadata {
// Define a class Student
class Student {
// Properties definition
public int RollNo
{
get;
set;
}
public string Name
{
get;
set;
}
// No Argument Constructor
public Student()
{
RollNo = 0;
Name = string.Empty;
}
// Parameterised Constructor
public Student(int rno, string n)
{
RollNo = rno;
Name = n;
}
// Method to Display Student Data
public void displayData()
{
Console.WriteLine("Roll Number : {0}", RollNo);
Console.WriteLine("Name : {0}", Name);
}
}
class GFG {
// Main Method
static void Main(string[] args)
{
// Declare Instance of class Assembly
// Call the GetExecutingAssembly method
// to load the current assembly
Assembly executing = Assembly.GetExecutingAssembly();
// Array to store types of the assembly
Type[] types = executing.GetTypes();
foreach(var item in types)
{
// Display each type
Console.WriteLine("Class : {0}", item.Name);
// Array to store methods
MethodInfo[] methods = item.GetMethods();
foreach(var method in methods)
{
// Display each method
Console.WriteLine("--> Method : {0}", method.Name);
// Array to store parameters
ParameterInfo[] parameters = method.GetParameters();
foreach(var arg in parameters)
{
// Display each parameter
Console.WriteLine("----> Parameter : {0} Type : {1}",
arg.Name, arg.ParameterType);
}
}
}
}
}
}
Output:
Class : Student
--> Method : get_RollNo
--> Method : set_RollNo
----> Parameter : value Type : System.Int32
--> Method : get_Name
--> Method : set_Name
----> Parameter : value Type : System.String
--> Method : displayData
--> Method : ToString
--> Method : Equals
----> Parameter : obj Type : System.Object
--> Method : GetHashCode
--> Method : GetType
Class : Program
--> Method : ToString
--> Method : Equals
----> Parameter : obj Type : System.Object
--> Method : GetHashCode
--> Method : GetType
Similar Reads
Late Binding using Reflection in C# The two main terms appearing in the above topic are Late Binding and reflection. So let us first define these two terms. The binding of methods and objects during run time is called Late Binding or Dynamic Binding. Reflection is the ability of an assembly to inspect its metadata. Metadata contains t
5 min read
Introduction to C# C# (C-sharp) is a modern, object-oriented programming language developed by Microsoft as part of the .NET framework. It was first released in 2000 and it has become one of the most widely used languages for building Windows applications, web services, and more. C# combines the power of C and C++ wit
7 min read
C# Restrictions on Properties Properties in C# are special class members that provide a flexible mechanism to read, write, or compute the value of a private field. They act as methods called accessors i.e. "get" and "set" methods. Using Properties, we can achieve encapsulation and information hiding. Properties allow controlled
5 min read
C# | Collection Class .math-table { border-collapse: collapse; width: 100%; } .math-table td { border: 1px solid #5fb962; text-align: left !important; padding: 8px; } .math-table th { border: 1px solid #5fb962; padding: 8px; } .math-table tr>th{ background-color: #c6ebd9; vertical-align: middle; } .math-table tr:nth-chil
5 min read
ref in C# The ref keyword in C# is used for passing or returning references of values to or from Methods. Basically, it means that any change made to a value that is passed by reference will reflect this change since you are modifying the value at the address and not just the value. It can be implemented in t
4 min read
C# | Generics - Introduction Generic is a class which allows the user to define classes and methods with the placeholder. Generics were added to version 2.0 of the C# language. The basic idea behind using Generic is to allow type (Integer, String, ⦠etc and user-defined types) to be a parameter to methods, classes, and interfac
6 min read