System.Reflection namespace in C#

The System.Reflection namespace in C# provides classes and interfaces that allow you to examine and interact with assemblies, modules, types, and members at runtime. This powerful feature enables reflection ? the ability to inspect and manipulate code dynamically during execution.

The Assembly class is central to this namespace, representing a loaded assembly in memory. You can access an assembly through a type's Assembly property or load it dynamically using various methods.

Assembly Identity Components

An assembly's identity consists of four key components −

  • Simple name − the filename without the extension
  • Version − from the AssemblyVersion attribute in major.minor.build.revision format (0.0.0.0 if absent)
  • Culture − localization information (neutral if not a satellite assembly)
  • Public key token − security identifier (null if not strongly named)

Syntax

The fully qualified assembly name follows this format −

simple-name, Version=version, Culture=culture, PublicKeyToken=public-key

To get an assembly from a type −

Assembly assembly = typeof(ClassName).Assembly;

To load an assembly dynamically −

Assembly assembly = Assembly.Load("assemblyName");

Getting Assembly Information

Example

using System;
using System.Reflection;

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

public class Program {
    public static void Main() {
        Assembly assembly = typeof(Person).Assembly;
        
        Console.WriteLine("Full Name: " + assembly.FullName);
        Console.WriteLine("Location: " + assembly.Location);
        Console.WriteLine("Simple Name: " + assembly.GetName().Name);
        Console.WriteLine("Version: " + assembly.GetName().Version);
    }
}

The output of the above code is −

Full Name: temp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
Location: /tmp/temp.dll
Simple Name: temp
Version: 1.0.0.0

Examining Types in an Assembly

Example

using System;
using System.Reflection;

public class Student {
    public string Name;
    public void Study() { }
    public void TakeExam() { }
}

public class Teacher {
    public string Subject;
    public void Teach() { }
}

public class Program {
    public static void Main() {
        Assembly assembly = typeof(Program).Assembly;
        Type[] types = assembly.GetTypes();
        
        Console.WriteLine("Types in assembly:");
        foreach (Type type in types) {
            Console.WriteLine("- " + type.Name);
            
            // Get methods
            MethodInfo[] methods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
            foreach (MethodInfo method in methods) {
                Console.WriteLine("  Method: " + method.Name);
            }
        }
    }
}

The output of the above code is −

Types in assembly:
- Student
  Method: Study
  Method: TakeExam
- Teacher
  Method: Teach
- Program

Dynamic Assembly Loading

Example

using System;
using System.Reflection;

public class Calculator {
    public static int Add(int a, int b) {
        return a + b;
    }
    
    public static int Multiply(int a, int b) {
        return a * b;
    }
}

public class Program {
    public static void Main() {
        // Get current assembly
        Assembly assembly = Assembly.GetExecutingAssembly();
        
        // Get the Calculator type
        Type calculatorType = assembly.GetType("Calculator");
        
        if (calculatorType != null) {
            // Get the Add method
            MethodInfo addMethod = calculatorType.GetMethod("Add");
            
            // Invoke the method dynamically
            object result = addMethod.Invoke(null, new object[] { 10, 20 });
            Console.WriteLine("Dynamic method call result: " + result);
            
            // Get all static methods
            MethodInfo[] methods = calculatorType.GetMethods(BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly);
            Console.WriteLine("\nStatic methods in Calculator:");
            foreach (MethodInfo method in methods) {
                Console.WriteLine("- " + method.Name);
            }
        }
    }
}

The output of the above code is −

Dynamic method call result: 30

Static methods in Calculator:
- Add
- Multiply

Common Assembly Methods

Method Description
GetTypes() Returns all types defined in the assembly
GetType(string) Gets a specific type by name
GetExecutingAssembly() Gets the assembly of the currently executing code
LoadFrom(string) Loads assembly from a file path

Conclusion

The System.Reflection namespace provides powerful capabilities for runtime code inspection and dynamic loading. It enables you to examine assembly metadata, discover types and members, and invoke methods dynamically, making it essential for building flexible and extensible applications.

Updated on: 2026-03-17T07:04:36+05:30

684 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements