Swiftorial Logo
Home
Swift Lessons
Tutorials
Learn More
Career
Resources

Using Reflection in C#

Introduction to Reflection

Reflection in C# is a powerful feature that allows you to obtain information about assemblies, modules, and types at runtime. With reflection, you can dynamically create instances of types, bind to methods, and access fields and properties.

Getting Started with Reflection

To use reflection, you need to include the System.Reflection namespace. This namespace provides classes such as Assembly, Type, MethodInfo, and PropertyInfo to work with reflection.

Example:

using System;
using System.Reflection;

class Program
{
    static void Main()
    {
        Type type = typeof(MyClass);
        Console.WriteLine("Class Name: " + type.Name);
    }
}

class MyClass
{
    public int MyProperty { get; set; }
    public void MyMethod() { }
}
                

Obtaining Type Information

One of the primary uses of reflection is to obtain type information at runtime. You can use the Type class to get information about a class, such as its methods, properties, fields, and events.

Example:

using System;
using System.Reflection;

class Program
{
    static void Main()
    {
        Type type = typeof(MyClass);
        Console.WriteLine("Class Name: " + type.Name);

        Console.WriteLine("Properties:");
        foreach (PropertyInfo prop in type.GetProperties())
        {
            Console.WriteLine(prop.Name);
        }

        Console.WriteLine("Methods:");
        foreach (MethodInfo method in type.GetMethods())
        {
            Console.WriteLine(method.Name);
        }
    }
}

class MyClass
{
    public int MyProperty { get; set; }
    public void MyMethod() { }
}
                

Creating Instances Using Reflection

Reflection allows you to create instances of types at runtime using the Activator.CreateInstance method. This is particularly useful when you don't know the type at compile time.

Example:

using System;

class Program
{
    static void Main()
    {
        Type type = typeof(MyClass);
        object obj = Activator.CreateInstance(type);
        Console.WriteLine("Instance created of type: " + obj.GetType().Name);
    }
}

class MyClass
{
    public int MyProperty { get; set; }
    public void MyMethod() { }
}
                

Invoking Methods Using Reflection

With reflection, you can invoke methods dynamically. This is done using the MethodInfo class and its Invoke method.

Example:

using System;
using System.Reflection;

class Program
{
    static void Main()
    {
        Type type = typeof(MyClass);
        object obj = Activator.CreateInstance(type);

        MethodInfo method = type.GetMethod("MyMethod");
        method.Invoke(obj, null);
    }
}

class MyClass
{
    public void MyMethod()
    {
        Console.WriteLine("MyMethod invoked!");
    }
}
                

Accessing Fields and Properties Using Reflection

Reflection also allows you to access fields and properties of an object at runtime. You can use the FieldInfo and PropertyInfo classes to achieve this.

Example:

using System;
using System.Reflection;

class Program
{
    static void Main()
    {
        Type type = typeof(MyClass);
        object obj = Activator.CreateInstance(type);

        PropertyInfo property = type.GetProperty("MyProperty");
        property.SetValue(obj, 42);
        Console.WriteLine("MyProperty: " + property.GetValue(obj));
    }
}

class MyClass
{
    public int MyProperty { get; set; }
}
                

Conclusion

Reflection is a powerful feature in C# that allows you to inspect and interact with objects at runtime. It provides a lot of flexibility but should be used judiciously due to potential performance overheads and security risks. With the examples provided, you should have a good starting point to explore reflection in your own projects.