A Comprehensive Resource for Microsoft Technologies

Welcome, your go-to destination for everything related to .NET and Microsoft technologies. With over 10 years of experience in the IT industry, I am excited to share my knowledge and insights on this platform. This blog is dedicated to providing valuable information, tips, and tutorials that are not only relevant to the rapidly evolving IT industry but also essential for individuals working on real-time projects and preparing for interviews

C# Tutorial 13: Reflection

 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:

using System.Reflection;

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 type
        Console.WriteLine("Type: " + myType.FullName);

        // Display the methods of the type
        MethodInfo[] methods = myType.GetMethods();
        Console.WriteLine("Methods:");
        foreach (var method in methods)
        {
            Console.WriteLine(method.Name);
        }

        // Display the properties of the type
        PropertyInfo[] 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 type
        object instance = Activator.CreateInstance(myType);

        // Cast the instance to the actual type
        MyClass myInstance = (MyClass)instance;

        // Use the instance
        myInstance.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 dynamically
        MethodInfo method = myType.GetMethod("MyMethod");
        method.Invoke(instance, null);

        // Access a property dynamically
        PropertyInfo property = myType.GetProperty("MyProperty");
        property.SetValue(instance, 42);

        // Display the updated property value
        Console.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.