What is Reflection?

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:

  1. Getting the type information of MyClass using typeof(MyClass).
  2. Using reflection to retrieve information about the properties and methods of MyClass using the GetProperties and GetMethods methods of the Type class.
  3. Printing the names of the properties and methods.
  4. Creating an instance of MyClass dynamically using Activator.CreateInstance.
  5. Setting the value of MyProperty using reflection by retrieving the property using GetProperty and then using SetValue to assign a value.
  6. Getting the value of MyProperty using reflection by retrieving the property and then using GetValue to retrieve the value.
  7. Invoking the MyMethod using reflection by retrieving the method using GetMethod and then using Invoke 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.

Post a Comment

Previous Post Next Post