✨ Shield now has support for Avalonia UI

Understanding C# Reflection

Jun 3, 2023 | C#, .NET


C# What is Reflection?

Before diving into the fun part, let’s answer this first – what is reflection in C#? Reflection is a powerful feature in .NET that enables you to analyze, inspect, and interact with the metadata of types, objects, and assemblies during runtime. In simpler terms, it allows your program to “think” and gain insights about itself or other types, properties, methods, etc., dynamically.

But wait, why would you ever need such a godly power? Let’s explore its purpose, benefits, and key components to start unlocking the secrets of reflection in C#.

The Purpose and Benefits of Using C# Reflection

Reflection plays a vital role in scenarios where you want to:

  • Inspect the structure and metadata of types and assemblies at runtime.
  • Load an assembly without referencing or compile-time dependencies.
  • Create instances of objects and invoke methods or instantiate types dynamically.
  • Implement runtime code generation, serialization, or design patterns based on interfaces, attributes or naming conventions.

Some of the most significant benefits of reflection include:

  • Flexibility: Create versatile applications that adapt to different contexts and type variations.
  • Extensibility: Enable plug-in-based architectures and reduce compile-time dependencies.
  • Dynamism: Perform tasks that are simply impossible through conventional coding approaches.

C# Reflection Key Components

Reflection stands on three major pillars in C#:

  1. Assemblies: Executable modules (usually with .dll or .exe extensions) containing compiled code, resources, and metadata.
  2. Types: Classes, interfaces, enums, structures, and delegates that hold metadata and can be used to generate objects or interact with members.
  3. Members: Properties, fields, methods, events, and constructors associated with a Type.

Now that we’ve got a basic understanding, let’s move on to delve into the abyss of C# reflection.

Delving into C# Reflection

Using System.Reflection in C#

System.Reflection is a fundamental namespace, loaded with classes that allow you to work with reflection. To start, add the following directive at the top of your code file:

using System.Reflection;

Once that’s done, you can perform various reflection tasks like loading assemblies, examining types and members, or creating dynamic instances.

Exploring C# Object Methods

All types in C# derive from the System.Object class, which has four methods directly connected to reflection.

  1. GetType(): Gets the Type of the current instance.
  2. MemberwiseClone(): Creates a shallow copy of the current object.
  3. GetHashCode(): Generates a hashcode for the current object.
  4. ToString(): Returns a string representation of the current object.

The most critical reflection method here is GetType(). Let’s see how to use it.

int someNumber = 42;
Type numberType = someNumber.GetType();
Console.WriteLine(numberType.FullName); // System.Int32

By using GetType() on the someNumber variable, we can get the Type object associated with it (System.Int32), providing a gateway to further reflection possibilities.

C# Get Method Name and Class Information

To extract or manipulate metadata from a Type object, use the properties and methods provided by the Type class.

// Using typeof keyword
Type exampleType = typeof(String);
Console.WriteLine(exampleType.FullName); // System.String

// Get properties & methods from Type
PropertyInfo[] properties = exampleType.GetProperties();
MethodInfo[] methods = exampleType.GetMethods();

// Display property names
foreach (PropertyInfo prop in properties)

// Get the method name in C#
foreach (MethodInfo method in methods)

The above example uses the typeof keyword to get the Type object for System.String. Then, it retrieves an array of PropertyInfo and MethodInfo, iterating through them to display the property and method names.

Get Assembly Details in C#

To load an assembly and extract its metadata, you can use the Assembly class and its various methods.

// Get the current assembly
Assembly currentAssembly = Assembly.GetExecutingAssembly();

// Get the assembly details
Console.WriteLine($"FullName: {currentAssembly.FullName}");
Console.WriteLine($"Location: {currentAssembly.Location}");

// Getypes from assembly
Type[] assemblyTypes = currentAssembly.GetTypes();
foreach (Type type in assemblyTypes)

The example gets the current executing assembly, prints out its full name and location, and iterates through the types available in the assembly, displaying their full names.

Reflection in C#: Static vs. Non-Static Members

Type members can be both static and non-static; hence you must consider what is static in C# when dealing with reflection.

Static members belong to the type itself, and you can access them without creating an instance of the type. Non-static members belong to the instances of a type, meaning you must create an instance (an object) first to access them.

While iteratively dealing with type members using reflection, it’s crucial to know the difference and handle the members accordingly.

C# Create Instance of Class Dynamically

Creating instances of objects during runtime can be an essential part of reflection. To create an instance of a class without knowing its type at compile-time, you can use the Activator.CreateInstance() method.

Type exampleType = typeof(List<int>);
object exampleInstance = Activator.CreateInstance(exampleType);

The Activator.CreateInstance() method returns an object, which you can later cast to a more specific type if required. In the example above, we create an instance of List<int> dynamically without explicitly knowing its type.

C# Activator and Its Role in Reflection

The Activator class plays a crucial role in reflection when you need to instantiate an object for an unknown type at compile-time. It provides methods to create types of objects locally or remotely, or obtain references to existing remote objects.

You can create instances of objects with specific constructor arguments, in a different application domain, or with specific activation attributes – all thanks to Activator’s methods.

Practical C# Reflection Examples

Reflection C# Example: Inspecting Class Properties

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

// Get Type of ExampleClass
Type classType = typeof(ExampleClass);

// Iterate through properties and get names & types
foreach (PropertyInfo property in classType.GetProperties())
    Console.WriteLine($"{property.Name} ({property.PropertyType.Name})");

In this reflection C# example, we have a simple ExampleClass with two properties: Name and Age. We get its Type, then iterate through its properties, displaying the property name and the type of the property.

Reflector C#: Invoking Methods Dynamically

public class Calculator
    public int Add(int a, int b)
        return a + b;

// Get Type of Calculator class
Type calculatorType = typeof(Calculator);

// Create an instance of Calculator
object calculatorInstance = Activator.CreateInstance(calculatorType);

// Get the Add method
MethodInfo addMethod = calculatorType.GetMethod("Add");

// Invoke the Add method dynamically
object result = addMethod.Invoke(calculatorInstance, new object[] { 10, 20 });
Console.WriteLine($"10 + 20 = {result}"); // 10 + 20 = 30

In this reflector C# example, we have a Calculator class with an Add method. We create an instance of the Calculator using Activator.CreateInstance(). Then, we get the Add method using GetMethod("Add"), and finally, we invoke the Add method dynamically, passing the required parameters and displaying the result on the console.

C# Add Property to Dynamic Object Using Reflection

using System.Dynamic;

// Create a dynamic object
dynamic expando = new ExpandoObject();

// Add properties using dynamic keyword
expando.FirstName = "John";
expando.LastName = "Doe";

// Use PropertyInfo to add properties by reflection
Type expandoType = expando.GetType();
PropertyInfo ageProperty = expandoType.GetProperty("Age");
if (ageProperty == null)
    IDictionary<string, object> expandoDict = (IDictionary<string, object>)expando;
    expandoDict["Age"] = 30;

Console.WriteLine($"{expando.FirstName} {expando.LastName}, {expando.Age} years old");

In this example, we demonstrate how to add property to a dynamic object using reflection. We create a dynamic object using ExpandoObject and add properties using the dynamic keyword. For reflection usage, we get the ExpandoObject type, check if the “Age” property exists, and add it to the dynamic object if it doesn’t exist already. Finally, we display the properties on the console.

C Sharp Reflection: Loading Assemblies and Types

// Load an assembly (replace with actual assembly file)
Assembly externalAssembly = Assembly.LoadFile(@"C:\path\to\your\assembly.dll");

// Get all the types in the assembly
Type[] assemblyTypes = externalAssembly.GetTypes();

// Find a specific type by its name
Type targetType = assemblyTypes.FirstOrDefault(t => t.Name == "TargetTypeName");

// Create an instance of the targetType (if found)
if (targetType != null)
    object targetInstance = Activator.CreateInstance(targetType);

In this C Sharp reflection example, we load an external assembly from a file path (replace with an actual assembly file path) using Assembly.LoadFile(). Afterward, we retrieve all types within the assembly, find a specific type by its name using LINQ FirstOrDefault(), and then create an instance of the target type using Activator.CreateInstance().

Advanced C# Reflection Techniques

C# Reflection Tutorial: Custom Attributes and Metadata

Attributes are nothing but metadata applied to programs. They allow you to add extra information to types, members or assemblies that can later be queried, validated, or manipulated through reflection.

// Custom attribute definition
public class MyCustomAttribute : Attribute
    public string Description { get; }

    public MyCustomAttribute(string description)
        Description = description;

[MyCustomAttribute("This is a custom attribute applied to a class.")]
public class ExampleClass
    // ...

// Get the custom attribute from ExampleClass
Type exampleType = typeof(ExampleClass);
object[] attributes = exampleType.GetCustomAttributes(typeof(MyCustomAttribute), false);
if (attributes.Length > 0)
    MyCustomAttribute customAttribute = (MyCustomAttribute)attributes[0];
    Console.WriteLine($"Custom Attribute: {customAttribute.Description}");

In this C# reflection tutorial, we create a MyCustomAttribute class inheriting from the Attribute class. We then apply this custom attribute to an ExampleClass and later query it using the GetCustomAttributes() method. Once retrieved, we cast the attribute to its specific type and access its properties.

NET Reflection C#: Manipulating Class Members at Runtime

Using reflection in C# can often lead you to manipulate class members during runtime. This can be useful for injecting custom logic, modifying object structures, or implementing cross-cutting concerns.

public class Employee
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public void PrintFullName()
        Console.WriteLine($"{FirstName} {LastName}");

public class UpperCaseEmployee : Employee
    public new void PrintFullName()
        // Use the base implementation for the actual logic

        // Additional logic: Change the names to uppercase
        FirstName = FirstName.ToUpper();
        LastName = LastName.ToUpper();

// Instantiate UpperCaseEmployee
UpperCaseEmployee employee = new UpperCaseEmployee { FirstName = "John", LastName = "Doe" };

// Use reflection to call PrintFullName() method
employee.GetType().GetMethod("PrintFullName").Invoke(employee, null);
Console.WriteLine($"{employee.FirstName} {employee.LastName}"); // JOHN DOE

In the above NET reflection C# example, we demonstrate how to manipulate class members at runtime. We have a base Employee class with FirstName, LastName, and a PrintFullName() method. The UpperCaseEmployee class inherits from Employee, hiding and extending the PrintFullName() method to change the names to uppercase. Finally, we create an instance of UpperCaseEmployee, use reflection to invoke the PrintFullName() method, and display the result on the console.

Using Reflection in C# to Generate Code

Reflection can play a pivotal role when it comes to code generation. You can inspect types, objects, or assemblies, and based on their metadata, generate code snippets or whole classes/structures during runtime.

public class Customer
    public string FirstName { get; set; }
    public int BirthYear { get; set; }

public static void GenerateObjectInitializer(Type targetType)
    StringBuilder builder = new StringBuilder($"new {targetType.Name}\n{{\n");
    PropertyInfo[] properties = targetType.GetProperties();

    foreach (PropertyInfo property in properties)
        builder.AppendLine($"{property.Name} = default({property.PropertyType.Name}),");

    string generatedCode = builder.ToString();

// Call the GenerateObjectInitializer method

// Output:
// new Customer
// {
//     FirstName = default(String),
//     BirthYear = default(Int32),
// };

In the above example, we demonstrate how using reflection in C# can generate code. The GenerateObjectInitializer() method takes a Type parameter and generates the corresponding object initializer code for that type. We then call this method, passing typeof(Customer) in, and see the generated code printed on the console.

C# Reflection Explained: Code Generation, Performance, and Security

When using reflection for code generation, keep in mind the following key aspects:

  • Performance: Reflection can be slower compared to conventional coding practices. Excessive usage or relentless iteration may lead to performance bottlenecks. Optimize your code and use caching whenever possible.
  • Security: Reflection may expose private members, providing inappropriate access, or expose sensitive information. Verify the trustworthiness of the source and use security attributes (e.g., SecurityTransparent and SecurityCritical) when dealing with potentially unsafe operations.

Reflection in C# for Dynamic Programming

Dynamic Method Dispatching in C#

Dynamic dispatching refers to the process of determining which method implementation to execute at runtime, rather than at compile-time.

Using dynamic objects (ExpandoObject, DynamicObject, or the dynamic keyword) with reflection allows additional flexibility during runtime, as shown in the following example:

using System.Dynamic;

dynamic dynamicInstance = new ExpandoObject();

// Add properties to dynamic instance
dynamicInstance.FirstName = "John";
dynamicInstance.LastName = "Doe";

// Get FullName dynamically
Func<dynamic, string> getFullName = d => $"{d.FirstName} {d.LastName}";
Console.WriteLine(getFullName(dynamicInstance)); // John Doe

Here, we create a dynamic object called dynamicInstance using ExpandoObject. We then add properties, such as FirstName and LastName, and declare a Func<dynamic, string> delegate named getFullName that takes a dynamic object as input and returns the full name string. Finally, we invoke the getFullName delegate using the dynamic instance.

C# Class Reflection and Extension Methods

Extension methods provide a unique way to add functionality to a class or type without modifying its source code. Combine this with reflection techniques, and you can create adaptive and dynamic code pieces to extend any given class or type based on metadata.

public static class TypeExtensions
    public static PropertyInfo[] GetPublicProperties(this Type targetType)
        return targetType.GetProperties(BindingFlags.Public | BindingFlags.Instance);

public class ExampleClass
    public int Property1 { get; set; }
    public string Property2 { get; set; }
    private double Property3 { get; set; }

// Utilize the extension method
Type exampleType = typeof(ExampleClass);
PropertyInfo[] publicProperties = exampleType.GetPublicProperties();
foreach (PropertyInfo property in publicProperties)
    Console.WriteLine($"{property.Name} ({property.PropertyType.Name})");

In this example, we extend the Type class through the TypeExtensions static class and the GetPublicProperties() extension method. Then, we have an ExampleClass with three properties, and we use reflection to get an instance of the class and call our extension method, which only returns the public properties.

Reflection Class C#: Working with Generics and Interfaces

Reflection can be useful to interrogate generic types, create instances of generic classes, or determine if a given type implements a specific interface.

public interface IMyInterface
    // ...

public class MyClass<T> : IMyInterface
    // ...

// Check if MyClass<> implements IMyInterface
Type myClassType = typeof(MyClass<>);
bool isImplemented = myClassType
    .Any(interfaceType => interfaceType == typeof(IMyInterface));
Console.WriteLine($"MyClass<> implements IMyInterface? {isImplemented}"); // True

// Create an instance of MyClass<int>
Type closedType = myClassType.MakeGenericType(typeof(int));
IMyInterface myInterfaceInstance = (IMyInterface)Activator.CreateInstance(closedType);

In this example, we have a generic class MyClass<T> that implements the IMyInterface interface. Using reflection, we first determine if the generic class implements the interface and print the result to the console. We then create an instance of MyClass<int> using MakeGenericType() and cast it to IMyInterface.

Reflection NET C#: Dynamic Proxy Implementation

A dynamic proxy is an object that mimics the behavior of a target object while providing additional functionality, such as logging, validation, or other cross-cutting concerns. You can achieve dynamic proxying with C# reflection, making it an excellent approach to aspect-oriented programming or on-the-fly behavior modification.

public class LogInterceptor : RealProxy
    private readonly object _targetInstance;

    public LogInterceptor(object targetInstance)
        : base(targetInstance.GetType())
        _targetInstance = targetInstance;

    public override IMessage Invoke(IMessage inputMessage)
        // Before invoking the method
        Console.WriteLine($"Before invoking method: {inputMessage.Properties["__MethodName"]}");

        IMethodCallMessage callMessage = inputMessage as IMethodCallMessage;
        object result = null;
        Exception exception = null;

            result = callMessage.MethodBase.Invoke(_targetInstance, callMessage.Args);
        catch (Exception ex)
            exception = ex;

        // After invoking the method
        Console.WriteLine($"After invoking method: {inputMessage.Properties["__MethodName"]}");

        return new ReturnMessage(result, null, 0, callMessage.LogicalCallContext, callMessage);

//Create a simple target class with a method to test logging
public class MyClass
    public void MyMethod()
        Console.WriteLine("MyMethod() is being executed.");

// Instantiate the target class and generate a proxy for logging
MyClass targetInstance = new MyClass();
LogInterceptor logInterceptor = new LogInterceptor(targetInstance);
IMyInterface proxy = (IMyInterface)logInterceptor.GetTransparentProxy();

// Invoke the method on proxy

/* Output:
Before invoking method: MyMethod
MyMethod() is being executed.
After invoking method: MyMethod

In this reflection NET C# example, we create a LogInterceptor class that inherits from RealProxy. It receives a target object, intercepts method invocations, and provides logging before and after the method execution.

We then create a MyClass object implementing IMyInterface, and instantiate the LogInterceptor with our target instance. When invoking the target method through the generated proxy, it dynamically logs the method invocation before and after the actual execution.

C# Reflection Best Practices and Potential Drawbacks

Common Reflection Performance Concerns and Optimizations

While reflection offers a plethora of capabilities, it does come at the cost of performance in some cases. Here are some tips to mitigate performance issues:

  • Use the Type.GetType() or typeof operator instead of the more expensive Assembly.GetType() when possible.
  • Cache frequently used Type, MethodInfo, or PropertyInfo objects to reduce redundant lookups.
  • Use Delegate.CreateDelegate() to create strongly typed delegates for method invocations, improving performance over the traditional Invoke() method.
  • Prefer Expression trees over raw reflection for creating delegates, as they provide better performance and type safety.
// Using expressions to create an action delegate
public class Car
    public void Drive(int distance) { /*...*/ }

public static Action<Car, int> CreateDriveDelegate()
    Type targetType = typeof(Car);
    MethodInfo driveMethod = targetType.GetMethod("Drive");
    ParameterExpression carParam = Expression.Parameter(targetType, "car");
    ParameterExpression distanceParam = Expression.Parameter(typeof(int), "distance");
    MethodCallExpression driveCall = Expression.Call(carParam, driveMethod, distanceParam);
    Action<Car, int> driveDelegate = Expression.Lambda<Action<Car, int>>(driveCall, carParam, distanceParam).Compile();
    return driveDelegate;

// Usage
Car car = new Car();
Action<Car, int> driveDelegate = CreateDriveDelegate();
driveDelegate(car, 100); // Calls 'Drive' method with a distance of 100.

Here, we use expression trees to create an action delegate for the Drive method of the Car class.

Using Reflection C#: Proper Error Handling and Validation

When using reflection, it’s crucial to handle errors and validate inputs. Some tips to consider are:

  • Always check for null values when dealing with lookups or type/member retrievals. You can use the null conditional operator (?.) to simplify your code.
MethodInfo method = typeof(MyClass).GetMethod("MyMethod");
ParameterInfo[] parameters = method?.GetParameters(); // Returns null if 'method' is null
  • For type casting, use the as operator and then check for null instead of direct casting to avoid InvalidCastException.
MyClass testObject = someObject as MyClass;
if (testObject != null)
    // Do something with testObject
  • Verify input parameters and their types before invoking methods or properties through reflection. You can use the Type.IsAssignableFrom() method to check if a value can be assigned to a specific parameter type.
MethodInfo method = /*...*/;
object[] args = new object[] { 42, "Hello" }; // Args for the method
bool areParametersValid = true;

ParameterInfo[] parameters = method.GetParameters();
for (int i = 0; i < parameters.Length; i++)
    if (!parameters[i].ParameterType.IsAssignableFrom(args[i].GetType()))
        areParametersValid = false;

if (areParametersValid)
    object result = method.Invoke(/*...*/);
  • Refrain from exposing sensitive information or private members unless it is indispensable. If you need to access private members, consider using BindingFlags.NonPublic rather than making them public.

Reflection Programming C#: When to Use and When to Avoid

While reflection unlocks powerful capabilities, some scenarios may not demand its usage or may introduce complexity, performance, or security risks. Evaluate the requirements, trade-offs, and alternatives before going down the reflection path.

When to use reflection:

Developing a plugin architecture

Implement a plugin architecture where new functionality can be added without recompiling the application. With reflection, you can inspect and load external assemblies, making it possible to create a flexible, extensible system.

public interface IPlugin
    void Execute();

// Plugin1.dll
public class Plugin1 : IPlugin
    public void Execute() { /*...*/ }

// Plugin2.dll
public class Plugin2 : IPlugin
    public void Execute() { /*...*/ }

// Main App
public class PluginLoader
    public static List<IPlugin> LoadPlugins(string pluginDirectory)
        List<IPlugin> plugins = new List<IPlugin>();

        foreach (string fileName in Directory.GetFiles(pluginDirectory, "*.dll"))
            Assembly assembly = Assembly.LoadFile(fileName);
            foreach (Type type in assembly.GetTypes())
                if (typeof(IPlugin).IsAssignableFrom(type) && !type.IsAbstract)
                    IPlugin plugin = Activator.CreateInstance(type) as IPlugin;

        return plugins;

In this example, we use reflection to dynamically load and instantiate plugin classes that implement IPlugin at runtime.

Implementing a serialization library

Use reflection to automatically serialize objects of arbitrary types. You can inspect properties and attributes of the types to create a serialized representation appropriately.

public class JsonSerialization
    public static string Serialize(object obj)
        // Use reflection to inspect obj properties and create a JSON representation.

    public static T Deserialize<T>(string json)
        // Use reflection to create an object of type T and populate it with JSON data.

This example shows a skeleton of a JSON serialization library that uses reflection to serialize and deserialize objects of various types.

Generating code or implementing dynamic proxies at runtime

Use reflection to generate code or implement dynamic proxies at runtime for aspect-oriented programming or mocking during testing.

public class DynamicProxy<T> : DispatchProxy
    private T _target;

    public static T Create(T target)
        object proxy = Create<T, DynamicProxy<T>>();
        return (T) proxy;

    private void SetTarget(T target)
        _target = target;

    protected override object Invoke(MethodInfo targetMethod, object[] args)
        Console.WriteLine($"Invoking {targetMethod.Name}");
        object result = targetMethod.Invoke(_target, args);
        Console.WriteLine($"Finished invoking {targetMethod.Name}");
        return result;

This example demonstrates the creation of a dynamic proxy using the DispatchProxy class. It intercepts method calls, allowing for additional behavior like logging or caching.

When to avoid reflection:

Type and its members are known at compile-time

If the type and its members are known at compile-time, use conventional constructs such as constructors and direct method calls. Avoid reflection as it may introduce unnecessary complexity and reduce performance.

// Without reflection
MyClass obj = new MyClass();

// With reflection (not recommended)
Type myType = typeof(MyClass);
ConstructorInfo ctor = myType.GetConstructor(Type.EmptyTypes);
object myObj = ctor.Invoke(new object[] { });
MethodInfo method = myType.GetMethod("MyMethod");
method.Invoke(myObj, null);

As shown in the example, using reflection when the type and members are known may increase the code complexity and affect performance negatively.

Performance is critical

If performance is critical and the use of reflection brings considerable overhead, consider alternatives like code generation, generic types, or expression trees.

// Use a generic type instead of reflection
public class Repository<T> where T : class { /*...*/ }

// Use an expression tree instead of reflection
public Func<T, TResult> CreatePropertyGetter<T, TResult>(string propertyName)
    PropertyInfo property = typeof(T).GetProperty(propertyName);
    ParameterExpression param = Expression.Parameter(typeof(T), "obj");
    MemberExpression propertyExpression = Expression.Property(param, property);
    return Expression.Lambda<Func<T, TResult>>(propertyExpression, param).Compile();

These examples demonstrate how to use generic types and expression trees to provide more performant alternatives to reflection.

Reflection is not necessary

Don’t use reflection just for the sake of it. Use it only when it is necessary and adds a significant benefit; otherwise, stick to regular coding practices. Overusing reflection may lead to code that is hard to maintain, debug, and optimize. Always consider simpler alternatives and evaluate the trade-offs before employing reflection.

In conclusion, mastering C# reflection opens up a new world of possibilities in terms of introspection, dynamic programming, and code generation. By understanding and implementing the proper concepts and techniques, you can harness the full power of C# reflection to develop more flexible and powerful applications.

You May Also Like