Introduction to Reflection in C#
What is Reflection?
Reflection is a feature in C# that allows a program to inspect and interact with its own structure or metadata at runtime. This includes accessing information about assemblies, modules, and types (such as classes, interfaces, and structs). Reflection can be used for various purposes such as dynamic type creation, invoking methods, accessing fields and properties, and more.
Why Use Reflection?
Reflection is particularly useful in scenarios where you need to inspect or manipulate types that are not known at compile time. Some common use cases include:
- Building generic frameworks and libraries
- Creating dynamic proxies
- Performing late binding
- Developing custom serialization mechanisms
- Building tools and utilities such as debuggers and code analyzers
Basic Reflection Example
Let's start with a simple example to understand how reflection works in C#. Consider the following class:
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public void SayHello()
{
Console.WriteLine("Hello, my name is " + Name);
}
}
We can use reflection to inspect the properties and methods of the Person
class:
using System;
using System.Reflection;
public class ReflectionExample
{
public static void Main()
{
Type personType = typeof(Person);
Console.WriteLine("Properties of Person class:");
foreach (PropertyInfo prop in personType.GetProperties())
{
Console.WriteLine(prop.Name);
}
Console.WriteLine("\nMethods of Person class:");
foreach (MethodInfo method in personType.GetMethods())
{
Console.WriteLine(method.Name);
}
}
}
Output:
Properties of Person class:
Name
Age
Methods of Person class:
get_Name
set_Name
get_Age
set_Age
SayHello
ToString
Equals
GetHashCode
GetType
Accessing and Invoking Members
Reflection allows you to access and invoke members of a class dynamically. Here's how you can create an instance of the Person
class, set its properties, and invoke its method using reflection:
using System;
using System.Reflection;
public class ReflectionInvokeExample
{
public static void Main()
{
Type personType = typeof(Person);
object personInstance = Activator.CreateInstance(personType);
PropertyInfo nameProp = personType.GetProperty("Name");
PropertyInfo ageProp = personType.GetProperty("Age");
nameProp.SetValue(personInstance, "John Doe");
ageProp.SetValue(personInstance, 30);
MethodInfo sayHelloMethod = personType.GetMethod("SayHello");
sayHelloMethod.Invoke(personInstance, null);
}
}
Output:
Hello, my name is John Doe
Conclusion
Reflection in C# is a powerful feature that allows programs to inspect and manipulate their own structure at runtime. It is useful for building dynamic and flexible applications, but it should be used with caution due to potential performance overhead and security risks.
We have covered the basics of reflection, including what it is, why it is useful, and how to use it to inspect and invoke members of a class. With this knowledge, you can start exploring more advanced reflection techniques and use cases in your applications.