Reflection is a powerful feature in C# that allows you to inspect and manipulate types, objects, properties, and methods at runtime. It provides the ability to dynamically examine and modify the structure, behavior, and metadata of types, even if they are not known at compile-time.
- Reflection is the ability of a code to access the metadata of the assembly during runtime.
- Metadata is information about data.
- Suppose you have to check the version of the assembly at runtime, then you can use reflection.
Here's an example that demonstrates the basics of reflection in C#:
using System;
using System.Reflection;
public class MyClass
{
public int MyProperty { get; set; }
public void MyMethod()
{
Console.WriteLine("Hello, Reflection!");
}
}
public class Program
{
public static void Main()
{
// Get the type of MyClass
Type myClassType = typeof(MyClass);
// Get information about the properties of MyClass
PropertyInfo[] properties = myClassType.GetProperties();
// Print the name of each property
Console.WriteLine("Properties:");
foreach (PropertyInfo property in properties)
{
Console.WriteLine(property.Name);
}
// Get information about the methods of MyClass
MethodInfo[] methods = myClassType.GetMethods();
// Print the name of each method
Console.WriteLine("Methods:");
foreach (MethodInfo method in methods)
{
Console.WriteLine(method.Name);
}
// Create an instance of MyClass using reflection
object myClassInstance = Activator.CreateInstance(myClassType);
// Set the value of MyProperty using reflection
PropertyInfo myProperty = myClassType.GetProperty("MyProperty");
myProperty.SetValue(myClassInstance, 42);
// Get the value of MyProperty using reflection
int myPropertyValue = (int)myProperty.GetValue(myClassInstance);
Console.WriteLine($"MyProperty value: {myPropertyValue}");
// Invoke the MyMethod using reflection
MethodInfo myMethod = myClassType.GetMethod("MyMethod");
myMethod.Invoke(myClassInstance, null);
}
}
In this example, we have a class named MyClass
with a property MyProperty
and a method MyMethod
. The Main
method demonstrates reflection by performing the following tasks:
- Getting the type information of
MyClass
usingtypeof(MyClass)
. - Using reflection to retrieve information about the properties and methods of
MyClass
using theGetProperties
andGetMethods
methods of theType
class. - Printing the names of the properties and methods.
- Creating an instance of
MyClass
dynamically usingActivator.CreateInstance
. - Setting the value of
MyProperty
using reflection by retrieving the property usingGetProperty
and then usingSetValue
to assign a value. - Getting the value of
MyProperty
using reflection by retrieving the property and then usingGetValue
to retrieve the value. - Invoking the
MyMethod
using reflection by retrieving the method usingGetMethod
and then usingInvoke
to call the method.
By using reflection, you can perform dynamic type inspection, create instances, access properties, invoke methods, and perform other operations at runtime, which can be useful in scenarios such as dynamic loading of types, plugin systems, or when interacting with unknown or dynamically generated types.