Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is System.Reflection.Module?

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

I know reflection pretty well and I've never heard of modules? What are they? the name is tantalizing.

like image 842
George Mauer Avatar asked May 21 '15 02:05

George Mauer


People also ask

What is System reflection used for?

Reflection Namespace. Contains types that retrieve information about assemblies, modules, members, parameters, and other entities in managed code by examining their metadata. These types also can be used to manipulate instances of loaded types, for example to hook up events or to invoke methods.

What is System reflection in C#?

Reflection is the process of describing the metadata of types, methods and fields in a code. The namespace System.Reflection enables you to obtain data about the loaded assemblies, the elements within them like classes, methods and value types.

WHAT IS modules in C sharp?

A module is a portable executable file, such as type. dll or application.exe, consisting of one or more classes and interfaces. There may be multiple namespaces contained in a single module, and a namespace may span multiple modules. One or more modules deployed as a unit compose an assembly.

What is System reflection assembly?

Namespace: System.Reflection. Summary. Defines an Assembly, which is a reusable, versionable, and self-describing building block of a common language runtime application.


Video Answer


2 Answers

An assembly can consist of multiple modules and even be split up into multiple files.

There are two primary use cases for using modules:

  • Combining modules written in different languages / compiled by different compilers in one assembly
  • Optimize downloading speed of an application by having a lightweight main module and loading further Types on demand.

Modules are in part an artifact of a time when the .NET team believed it was important for users to be able to download assemblies over the network to their local machine for execution.

To save bandwidth, an assembly can be split up into multiple files, each containing a module. The assembly file containing the primary or main module contains an assembly manifest that lists the location of all other assembly files. This allows a developer to ensure a fast initial download of the main assembly and the runtime loads Types or Resources from other modules in the assembly on demand.

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

Most assemblies today are single-module assemblies containing only a main module. Unless you write code dealing with Reflection (or raw IL) for a living (I did some time back), you'll be fine if you just do assembly.MainModule whenever required. I'm pretty sure the number of people using multil-file/multi-module is ɛ​ (only marginally larger than 0).

like image 159
Johannes Rudolph Avatar answered Oct 17 '22 21:10

Johannes Rudolph


A module is the unit of storage for executable code in .NET. An assembly is a collection of one or more modules. Explains the term "assembly", it is an assembly of modules. You very rarely encounter the concept, it is hidden by the build tools in .NET. But it is pretty visible when you look at the kind of code you need to dynamically generate code at runtime with the TypeBuilder class.

Note the code sample in the linked MSDN article. You start with an AssemblyBuilder. To which you add a ModuleBuilder. To which you add a TypeBuilder. To which you add fields, constructors, methods. Shows the internal hierarchy quite well.

You can create your own modules but that isn't directly supported by MSBuild, you have to run the build tools yourself. The C# compiler's /target:module command line option tells it to create a module. And you have to use the assembly linker, al.exe, to glue the modules together into an assembly. The only practical use of this that I can think of is to create assemblies that contain code written in different languages. Mixing C++/CLI and C# would be a practical example.

Almost all of the assemblies in every day use just contain a single module. The C# compiler hides the final step, creating the assembly, it directly calls the assembly linker at compile time. C:\Windows\Microsoft.NET\Framework\v4.0.30319\alink.dll on your machine.

like image 33
Hans Passant Avatar answered Oct 17 '22 21:10

Hans Passant