Module Class

Namespace: System.Reflection

Represents a loadable code module (such as a DLL or EXE file) within an assembly. This class cannot be inherited.

Introduction

The System.Reflection.Module class provides a way to inspect and interact with the metadata of a dynamically loaded assembly. It allows you to discover the types, methods, and other elements defined within a module. This class is fundamental for many reflection-based scenarios, such as serialization, ORM frameworks, and dynamic code generation.

Syntax

C#
public sealed class Module

Remarks

A module represents a single Portable Executable (PE) file (.dll or .exe) within an assembly. An assembly can consist of one or more modules. The Module class provides access to the metadata of these modules, allowing you to query for types, methods, fields, and other members defined within that specific module.

You can obtain a Module object through various reflection methods, such as calling Assembly.GetModule or Assembly.GetModules.

Members

Example

The following example demonstrates how to get the Module objects for an assembly and iterate through their types.

C#
using System;
using System.Reflection;

public class Example
{
    public static void Main(string[] args)
    {
        // Get the currently executing assembly
        Assembly currentAssembly = Assembly.GetExecutingAssembly();

        // Get all modules in the assembly
        Module[] modules = currentAssembly.GetModules();

        foreach (Module module in modules)
        {
            Console.WriteLine($"Module Name: {module.Name}");
            Console.WriteLine($"  Fully Qualified Name: {module.FullyQualifiedName}");
            Console.WriteLine($"  Scope Name: {module.ScopeName}");
            Console.WriteLine($"  GUID: {module.Guid}");

            // Get all types defined in this module
            try
            {
                Type[] types = module.GetTypes();
                Console.WriteLine("  Types defined:");
                foreach (Type type in types)
                {
                    Console.WriteLine($"    - {type.FullName}");
                }
            }
            catch (ReflectionTypeLoadException ex)
            {
                Console.WriteLine($"  Error loading types: {ex.Message}");
                foreach (Exception loaderException in ex.LoaderExceptions)
                {
                    Console.WriteLine($"    Loader Exception: {loaderException.Message}");
                }
            }
            Console.WriteLine();
        }
    }
}

See Also