c-sharp
  1. c-sharp-reflection

C# Reflection

C# Reflection allows you to access metadata and information about a particular type at runtime. With Reflection, you can create objects dynamically, inspect object types, create type instances, and execute object methods. In this tutorial, we'll discuss how to use Reflection in C#.

Syntax

The syntax for using Reflection in C# is as follows:

Type myType = typeof(MyClass);
MethodInfo myMethod = myType.GetMethod("MyMethod");

This code gets the type of a class called "MyClass" and retrieves its method called "MyMethod".

Example

Let's say we have a class called "Person", which has two properties: "Name" and "Age". We can use Reflection to create an instance of the "Person" class dynamically, set its properties, and invoke its methods:

using System.Reflection;

class Person {
   public string Name { get; set; }
   public int Age { get; set; }

   public void PrintDetails() {
      Console.WriteLine("Name: " + Name + ", Age: " + Age);
   }
}

class Program {
   static void Main(string[] args) {

      // Create an instance of Person dynamically
      Type type = typeof(Person);
      object instance = Activator.CreateInstance(type);

      // Set instance properties
      PropertyInfo nameProperty = type.GetProperty("Name");
      nameProperty.SetValue(instance, "John");

      PropertyInfo ageProperty = type.GetProperty("Age");
      ageProperty.SetValue(instance, 30);

      // Invoke instance method
      MethodInfo method = type.GetMethod("PrintDetails");
      method.Invoke(instance, null);
   }
}

Output

When we run the example code above, the output will be:

Name: John, Age: 30

This is because we created an instance of the "Person" class dynamically, set its properties and invoked its "PrintDetails" method.

Explanation

In the example above, we used Reflection to create an instance of the "Person" class dynamically, set its properties ("Name" and "Age"), and invoke its method ("PrintDetails"). We first got the type of the "Person" class using the typeof operator, and then created an instance of the class using the Activator class.

We then set the instance properties using the GetProperty method and the SetValue method. Finally, we invoked the instance method using the GetMethod and Invoke methods.

Use

Reflection is used in C# to inspect and manipulate types at runtime. It is useful when you need to access metadata about a type, or when you need to create objects dynamically. Reflection is often used in libraries and frameworks where the types being processed are not known at compile-time.

Important Points

  • Reflection can have a performance impact on your application, as it involves querying metadata at runtime.
  • Reflection should be used sparingly, as it can make code more complex and harder to maintain.
  • Reflection can be used to access private members of a class, which may lead to security vulnerabilities.

Summary

In this tutorial, we discussed how to use Reflection in C#. We covered the syntax, example, output, explanation, use, and important points of Reflection in C#. With this knowledge, you can now use Reflection in your C# code to access metadata about a type and create objects dynamically.

Published on: