Modules are the building blocks of .NET assemblies. Modules consist of a single file that contains the following:
-
Microsoft Intermediate Language (MSIL) code created from your C# source code during compilation
-
Metadata describing the types contained in the module
-
Resources, such as icons and string tables, used by the types in the module
Assemblies consist of one or more modules and an assembly manifest. When there is a single module, the module and assembly manifest are usually built into a single file for convenience. When there is more than one module, the assembly represents a logical grouping of more than one file that you must deploy as a complete unit. In these situations, the assembly manifest is either contained in a separate file or built into one of the modules.
By building an assembly from multiple modules, you complicate the management and deployment of the assembly, but under some circumstances, modules offer significant benefits, including:
-
The runtime will load a module only when the types defined in the module are required. Therefore, where you have a set of types that your application uses rarely, you can partition them into a separate module that the runtime will load only if necessary. This offers the following benefits:
-
Improved performance, especially if your application is loaded across a network.
-
Minimizing the use of memory.
-
-
The ability to use many different languages to write applications that run on the common language runtime (CLR) is a great strength of the .NET Framework. However, the C# compiler can't compile your Microsoft Visual Basic .NET or COBOL .NET code for inclusion in your assembly. You must first use a language-specific compiler to turn your source into MSIL in a structure that the C# compiler can incorporate—a module. Likewise, if you want to allow programmers of other languages to use the types you develop in C#, you must build them into a module.
To compile a source file named ConsoleUtils.cs into a module use the command csc /target:module ConsoleUtils.cs. The result is the creation of a file named ConsoleUtils.netmodule. The netmodule extension is the default extension for modules, and the file name is the same as the name of the C# source file.
You can also build modules from multiple source files, which results in a single file (module) containing the MSIL and metadata for all types contained in all the source files. The command csc /target:module ConsoleUtils.cs WindowsUtils.cs compiles two source files named ConsoleUtils.cs and WindowsUtils.cs to create the module named ConsoleUtils.netmodule. The module is named after the first source file listed unless you override the name with the /out compiler switch. For example, the command csc /target:module /out:Utilities.netmodule ConsoleUtils.cs WindowsUtils.cs creates a module named Utilities.netmodule.
To build an assembly consisting of multiple modules, you must use the /addmodule compiler switch. To build an executable named MyFirstApp.exe from two modules named WindowsUtils.netmodule and ConsoleUtils.netmodule and two source files named SourceOne.cs and SourceTwo.cs, use the command csc /out:MyFirstApp.exe /target:exe /addmodule:WindowsUtils.netmodule,ConsoleUtils.netmodule SourceOne.cs SourceTwo.cs. This command will result in an assembly consisting of the following files:
-
MyFirstApp.exe, which contains the assembly manifest as well as the MSIL for the types declared in the SourceOne.cs and SourceTwo source files.
-
ConsoleUtils.netmodule and WindowsUtils.netmodule, which are now integral components of the multi-file assembly but are unchanged by this compilation process. (If you attempt to run MyFirstApp.exe without the netmodules present, a System.IO.FileNotFoundException is thrown.)
No comments:
Post a Comment