Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the "<Module>" type?

I am using Mono.Cecil to read an assembly generated by Regex.CompileToAssembly(). When I iterate through the types, there is one type in the root namespace named <Module>. The type has no base type. What is this type? Is this some Mono.Cecil artifact or something that is actually a real part of .NET assemblies? What role does it play?

like image 251
ChaseMedallion Avatar asked Feb 25 '16 02:02

ChaseMedallion


1 Answers

The <Module> type is a place-holder for declarations that do not fit the CLI model. Normally relevant only in assemblies that are mixed-mode, containing both code written in a managed language as well as an unmanaged one like C or C++. It is empty for pure managed assemblies.

Those languages support free functions and global variables. The CLR does not directly support that, methods and variables must always be a member of a type. So the metadata generator uses a simple trick, it creates a fake type to be the home of such functions and variables. The name of that fake type is <Module>. It always has internal accessibility to hide the members. There is only ever one of those types, its RID is always 1.

The CLR source code calls it the "Global Class".

like image 194
Hans Passant Avatar answered Oct 17 '22 05:10

Hans Passant