Reflection is a powerful feature of C# that allows you to inspect and manipulate code at runtime. With reflection, you can dynamically create and execute code, retrieve information about types, fields, and methods, and even modify the behavior of objects and classes.
In this article, we’ll explore the basics of reflection in C# and provide some examples to help you get started.
What is Reflection?
Reflection is the ability of a program to inspect its own code and manipulate its own behavior. This is achieved through the use of metadata, which is information about the types, fields, and methods in a program. This metadata is stored in an assembly, which is a compiled unit of code.
With reflection, you can use this metadata to dynamically load and execute code, and to modify the behavior of objects and classes. This can be useful in a variety of scenarios, such as creating plugins, building object-oriented frameworks, and implementing advanced debugging tools.
How to Use Reflection in C#
To use reflection in C#, you typically use the System.Reflection namespace, which provides a range of classes and methods for working with metadata. Here are some basic steps to get started:
- Load the assembly: The first step in using reflection is to load the assembly that contains the metadata you want to inspect. You can do this using the Assembly.Load method, which takes the name of the assembly as a parameter.
- Get the type: Once you have loaded the assembly, you can use the Type.GetType method to get a Type object representing the type you want to inspect. This method takes the name of the type as a parameter.
- Inspect the type: With the Type object, you can inspect the fields, properties, and methods of the type using various methods provided by the System.Reflection.TypeInfo class. For example, you can use the GetFields method to get an array of FieldInfo objects representing the fields in the type.
- Create and execute code: With reflection, you can also dynamically create and execute code at runtime using the System.Reflection.Emit namespace. This allows you to generate code on the fly and execute it as if it were part of your program.
Example of Reflection in C#
Here is an example of using reflection to load an assembly, get a type, and invoke a method:
static void Main(string args)
// Load the assembly
Assembly assembly = Assembly.Load("MyAssembly");
// Get the type
Type type = assembly.GetType("MyNamespace.MyClass");
// Create an instance of the class
object instance = Activator.CreateInstance(type);
// Get the method
MethodInfo method = type.GetMethod("MyMethod");
// Invoke the method
In this example, we first load an assembly called “MyAssembly”. We then get a Type object representing a class called “MyClass” in the “MyNamespace” namespace. We create an instance of this class using the Activator.CreateInstance method, and then get a MethodInfo object representing a method called “MyMethod”.
Finally, we invoke the method using the Invoke method, passing in the instance object and any parameters the method requires.
Reflection is a powerful feature of C# that allows you to inspect and manipulate code at runtime. With reflection, you can dynamically create and execute code, retrieve information about types, fields, and methods, and even modify the behavior of objects and classes. By mastering this feature, you can build more flexible, extensible, and powerful applications in C#