Reflection in C# is a powerful feature that allows you to inspect and interact with the metadata of types (classes, interfaces, structs, enums, etc.) in your program during runtime. It provides a way to obtain information about assemblies, modules, and types, as well as to dynamically create and manipulate instances of types. In this tutorial, we'll cover the basics of reflection in C#.
1. Introduction to Reflection:
Reflection is part of the System.Reflection
namespace in C#. To get started, ensure you have the following using directive at the top of your file:
2. Obtaining Type Information:
You can use reflection to obtain information about types, such as class names, methods, properties, and more. Here's an example of how to get type information:
using System;class Program{static void Main(){Type myType = typeof(MyClass);// Display the full name of the typeConsole.WriteLine("Type: " + myType.FullName);// Display the methods of the typeMethodInfo[] methods = myType.GetMethods();Console.WriteLine("Methods:");foreach (var method in methods){Console.WriteLine(method.Name);}// Display the properties of the typePropertyInfo[] properties = myType.GetProperties();Console.WriteLine("Properties:");foreach (var property in properties){Console.WriteLine(property.Name);}}}class MyClass{public void MyMethod() { }public int MyProperty { get; set; }}
3. Creating Instances Dynamically:
Reflection allows you to create instances of types dynamically. Here's an example:
using System;class Program{static void Main(){Type myType = typeof(MyClass);// Create an instance of the typeobject instance = Activator.CreateInstance(myType);// Cast the instance to the actual typeMyClass myInstance = (MyClass)instance;// Use the instancemyInstance.MyMethod();myInstance.MyProperty = 42;}}class MyClass{public void MyMethod() { }public int MyProperty { get; set; }}
4. Invoking Methods and Accessing Properties:
Reflection can be used to invoke methods and access properties dynamically. Here's an example:
using System;using System.Reflection;class Program{static void Main(){Type myType = typeof(MyClass);object instance = Activator.CreateInstance(myType);// Invoke a method dynamicallyMethodInfo method = myType.GetMethod("MyMethod");method.Invoke(instance, null);// Access a property dynamicallyPropertyInfo property = myType.GetProperty("MyProperty");property.SetValue(instance, 42);// Display the updated property valueConsole.WriteLine("MyProperty: " + property.GetValue(instance));}}class MyClass{public void MyMethod() { }public int MyProperty { get; set; }}
5. Conclusion:
Reflection is a powerful feature that can be used for various purposes such as creating extensible applications, building generic libraries, and implementing frameworks. However, it should be used judiciously, as it can have a performance impact and make the code less maintainable if not used carefully.
Remember that while reflection provides flexibility, it's often better to use static typing whenever possible for better code readability and performance. Use reflection only when dynamic behavior is essential.
I hope this tutorial helps you get started with reflection in C#! If you have any questions or need further clarification, feel free to ask.