What is System.Reflection.Module?

Just noticed Assembly.LoadModule() in intellisense. I see that it returns a reference to the main undocumented class Module .

I know reflection well, and have I never heard of modules? What are they? the name is excruciating.

+7
reflection bcl
source share
2 answers

An assembly can consist of several modules and even split into several files.

There are two main options for using modules:

  • Combining modules written in different languages ​​/ compiled by different compilers in one assembly
  • Optimization of application loading speed with the help of a lightweight main module and additional types of loading on demand.

Modules are partly an artifact of the times when the .NET team considered it important that users could download assemblies over the network to their local machine for execution.

To save bandwidth, the assembly can be divided into several files, each of which contains a module. The assembly file containing the main or main module contains the assembly manifest , which indicates the location of all other assembly files. This allows the developer to provide quick initial loading of the main assembly, and runtime loads types or resources from other modules in the assembly on demand.

If you're interested, you can actually instruct the C # compiler to leak out the modules and compile them manually using the assembly linker. Here's a tutorial on MSDN .

Most assemblies today are single-module assemblies containing only the main module. If you are not writing Reflection (or raw IL) dedicated code for life (I did some time ago), you'll be fine if you just execute assembly.MainModule when necessary. I am sure that the number of people using multil-file / multi-module is Ι› (only slightly more than 0).

+6
source share

A module is a unit for storing executable code in .NET. An assembly is a collection of one or more modules. Explains the term "assembly", this is the assembly of modules. You rarely encounter a concept; it is hidden by the building tools in .NET. But this is noticeable when you look at the code that you need to dynamically generate code at runtime with the TypeBuilder class .

Check out the sample code in the related MSDN article. You start with AssemblyBuilder. To which you add ModuleBuilder. To which you add TypeBuilder. To which you add fields, constructors, methods. It shows the internal hierarchy well.

You can create your own modules, but not directly supported by MSBuild, you need to run the build tools yourself. The C # / target: module compiler indicates that it is creating a module. And you should use the layout linker, al.exe , to glue the modules together into the assembly. The only practical use of this that I can think of is to create assemblies containing code written in different languages. Mixing C ++ / CLI and C # would be a practical example.

Almost all builds use only one module every day. The C # compiler hides the last step, creating an assembly, it directly calls the linker assembler at compile time. C: \ Windows \ Microsoft.NET \ Framework \ v4.0.30319 \ alink.dll on your computer.

+5
source share

All Articles