Represents a loadable code module (such as a DLL or EXE file) within an assembly. This class cannot be inherited.
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.
public sealed class Module
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
.
Gets the name of the module.
This property returns the name of the module, typically the filename of the associated PE file.
Gets the scope name of the module.
The scope name is used for resolving type names within the module's scope.
Gets the fully qualified name of the module.
This property returns the full path or name of the module, which can be useful for identifying the module in the file system.
Gets the assembly that contains this module.
This property returns the Assembly
object to which this module belongs.
Gets a handle to the module.
The ModuleHandle
provides low-level access to the module's runtime representation.
Gets the globally unique identifier (GUID) of the module.
This GUID is unique across all modules and assemblies.
Gets the metadata token for this module.
The metadata token is a unique identifier for the module within the assembly's metadata stream.
Returns an array of all the types defined in this module.
This method enumerates all the types declared within the current module.
An array of Type
objects representing the types defined in this module. Returns an empty array if the module is empty.
ReflectionTypeLoadException
: If types cannot be loaded from the module.Searches for a type with the specified name in this module.
This method allows you to retrieve a specific Type
object by its fully qualified name.
name
The Type
object with the specified name, or null
if the type is not found.
Returns a collection of custom attributes that are defined for this module.
This method retrieves all attributes applied directly to the module itself.
An array of objects representing the custom attributes. Returns an empty array if no attributes are defined.
Returns a collection of custom attributes of a specified type that are defined for this module.
This method filters custom attributes by a specific attribute type.
attributeType
An array of objects representing the custom attributes of the specified type. Returns an empty array if no matching attributes are found.
Indicates whether a custom attribute of a specified type is defined for this module.
This method efficiently checks for the presence of a specific custom attribute type.
attributeType
true
if the attribute is defined; otherwise, false
.
The following example demonstrates how to get the Module
objects for an assembly and iterate through their types.
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();
}
}
}