Multiple File Assembly

The process of creating a multifile assembly takes some explaining, as does the use of some command line utilities. A key point to understand is that a multifile assembly can contain only one entry point. That is to say that only one class within the assembly can contain a Main method. Of course, if the multifile assembly is a DLL, this is not an issue. However, if the multifile assembly is an EXE, it is not possible for the assembly to contain more than one entry point.

To create the multifile assembly, it will be necessary to create two source files and compile them into code modules, which have the extension .netmodule by default. Listings 5.1.1 and 5.1.2 contain the source for very simple classes that will comprise the multifile assembly.

Listing 5.1.1. ClassOne of the Multi-File Assembly
 1: using System;
 2: using FileTwo;
 3:
 4: namespace FileOne {
 5:
 6:     public class ClassOne {
 7:
 8:         public static void Main( ) {
 9:
10:            ClassTwo ct = new ClassTwo( );
11:            ct.SayHello( );
12:
13:         }
14:     }
15: }

Listing 5.1.2. ClassTwo of the Multi-File Assembly
 1: using System;
 2:
 3:
 4: namespace FileTwo {
 5:
 6:
 7:     public class ClassTwo {
 8:
 9:         public void SayHello( ) {
10:
11:             Console.WriteLine( "Hello From FileTwo.ClassTwo" );
12:         }
13:     }
14: }

To create the new assembly, it is necessary to first compile each source file into a module. Note the order and steps of the compilation process:

1.
csc /t:module two.cs

2.
csc /addmodule:two.netmodule /t:module one.cs

3.
al one.netmodule two.netmodule /main:FileOne.ClassOne.Main/out:MultiAsm.exe /target:exe

The first step is the creation of a code module from the second source file. This is due to the fact that the first source module references FileTwo.ClassTwo. Next is the creation of the code module for the first source file, which references the code module created from the second source file. The final step involves using the Assembly Generation Tool (al.exe) to create the multifile assembly. Note that if you delete the .netmodule files and attempt to execute MultiAsm.exe, the runtime will generate an exception due to missing dependencies. Figure 5.1.2 shows the manifest for MultiAsm.exe; note the references to one.netmodule and two.netmodule.

Figure 5.1.2. Manifest for MultiAsm.exe.


By packaging multiple files together into an assembly, it is possible to ensure proper references and versions of the code being executed.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.0.145