What is the System.Reflection.Module in C#?

The System.Reflection.Module class in C# represents a module, which is a portable executable file containing one or more classes and interfaces. It is part of the System.Reflection namespace that allows you to examine and manipulate metadata about types, assemblies, and modules at runtime.

A module is essentially a single file within an assembly. Most .NET applications consist of a single module that matches the assembly, but complex applications can have multiple modules within one assembly.

Key Properties and Methods

The Module class provides several important properties and methods for reflection −

  • Assembly − Gets the assembly that contains the current module

  • Name − Gets the name of the module

  • FullyQualifiedName − Gets the fully qualified name and path to this module

  • GetTypes() − Returns an array of all types defined in this module

  • GetType(string) − Gets a specific type by name from the module

Getting Module Information

Example

using System;
using System.Reflection;

public class Sample {
    public void Display() {
        Console.WriteLine("Sample class method");
    }
}

class Program {
    static void Main(string[] args) {
        Type type = typeof(Sample);
        Module module = type.Module;
        
        Console.WriteLine("Module Name: " + module.Name);
        Console.WriteLine("Assembly: " + module.Assembly.GetName().Name);
        Console.WriteLine("Fully Qualified Name: " + module.FullyQualifiedName);
        
        Console.WriteLine("\nTypes in this module:");
        Type[] types = module.GetTypes();
        foreach (Type t in types) {
            Console.WriteLine("- " + t.Name);
        }
    }
}

The output of the above code is −

Module Name: ConsoleApp.exe
Assembly: ConsoleApp
Fully Qualified Name: C:\path\to\ConsoleApp.exe

Types in this module:
- Sample
- Program

Working with Custom Attributes via Module

Example

using System;
using System.Reflection;

[AttributeUsage(AttributeTargets.All)]
public class HelpAttribute : System.Attribute {
    public readonly string Url;
    public string Topic { get; set; }
    
    public HelpAttribute(string url) {
        this.Url = url;
    }
}

[Help("Information on the class MyClass", Topic = "Sample Topic")]
class MyClass {
    public void TestMethod() {
        Console.WriteLine("Test method called");
    }
}

class Program {
    static void Main(string[] args) {
        Type type = typeof(MyClass);
        Module module = type.Module;
        
        Console.WriteLine("Examining type: " + type.Name);
        Console.WriteLine("From module: " + module.Name);
        
        object[] attributes = type.GetCustomAttributes(true);
        Console.WriteLine("\nCustom attributes found: " + attributes.Length);
        
        foreach (object attr in attributes) {
            if (attr is HelpAttribute helpAttr) {
                Console.WriteLine("URL: " + helpAttr.Url);
                Console.WriteLine("Topic: " + helpAttr.Topic);
            }
        }
        
        MyClass obj = new MyClass();
        obj.TestMethod();
    }
}

The output of the above code is −

Examining type: MyClass
From module: ConsoleApp.exe

Custom attributes found: 1
URL: Information on the class MyClass
Topic: Sample Topic
Test method called

Module vs Assembly Relationship

Assembly Module
Logical unit of deployment and security Physical file that contains executable code
Can contain one or more modules Belongs to exactly one assembly
Has metadata and security permissions Contains type definitions and IL code
Loaded into AppDomain Loaded as part of assembly

Finding Specific Types in a Module

Example

using System;
using System.Reflection;

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

public class Department {
    public string DeptName { get; set; }
}

class Program {
    static void Main(string[] args) {
        Module currentModule = typeof(Program).Module;
        
        Console.WriteLine("Searching for specific types in module...
"); Type empType = currentModule.GetType("Employee"); if (empType != null) { Console.WriteLine("Found Employee type"); PropertyInfo[] props = empType.GetProperties(); Console.WriteLine("Properties:"); foreach (PropertyInfo prop in props) { Console.WriteLine("- " + prop.Name + " (" + prop.PropertyType.Name + ")"); } } Type deptType = currentModule.GetType("Department"); if (deptType != null) { Console.WriteLine("\nFound Department type"); PropertyInfo[] props = deptType.GetProperties(); Console.WriteLine("Properties:"); foreach (PropertyInfo prop in props) { Console.WriteLine("- " + prop.Name + " (" + prop.PropertyType.Name + ")"); } } } }

The output of the above code is −

Searching for specific types in module...

Found Employee type
Properties:
- Name (String)
- Age (Int32)

Found Department type
Properties:
- DeptName (String)

Conclusion

The System.Reflection.Module class provides essential functionality for examining modules at runtime, including retrieving type information, custom attributes, and metadata. It serves as a bridge between assemblies and the individual types they contain, making it crucial for dynamic type loading and reflection-based programming scenarios.

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

286 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements