19.4 Creating a Multifile Assembly

 <  Day Day Up  >  

You want to create an assembly in which the IL code is spread across a number of files.


Technique

Visual Studio.NET does not support the ability to generate multifile assemblies, so you need to compile from the command line. You must first compile the files that will form the additional modules using the csc command and the /target:module flag:

 
 csc /reference:  Assemblies referenced  /target:module  source files  

Finally, you compile the prime module (that is to say, the main assembly file, which contains the manifest), in the normal way, but using the /addmodule flag to inform the compiler about the additional modules in the assembly. You can specify multiple modules, separated by a semicolon: /addmodule:file1.netmodule;file2.netmodule . Note that .netmodule is the conventional extension for assembly module files. The actual effect of /addmodule is to cause the compiler to add the metadata (but not the IL code) from the specified modules to the generated assembly.

For example, suppose you have three source files, Main.cs , Rare1.cs , and Rare2.cs . Rare1.cs and Rare2.cs contain the source code for some rarely used types that will go into a separate module, whereas Main.cs contains the code that will go into the prime module. Suppose also that the code references System.dll and System. Windows .Forms.dll from both modules. You should in this case compile the complete assembly using these commands:

 
 csc /target:module /reference:system.dll;system.windows.forms.dll /out:rare.netmodule rare1.cs rare2.cs csc /target:winexe /reference:system.dll;system.windows.forms.dll /addmodule:rare.netmodule /out:main.exe main.cs 

Recall that the /out flag indicates the name of the file containing the assembly or module generated, and /target:winexe indicates that a .exe file containing a Windows Forms application should be generated. The full set of options are

  • /target:exe generates a console application.

  • /target:library generates a DLL assembly that contains no start method, so you cannot start it directly.

  • /target:winexe generates a Windows application.

  • /target:module generates a module, which must be included in an assembly before you can execute it.

After you execute these commands, the files main.exe and rare.netmodule are created, which between them contain the entire assembly.

Comments

As you might have guessed from the names used in the preceding example, the main purpose of creating multifile assemblies is to be able to separate rarely used code or resources. The idea is that if some code or resources are large but rarely used, then most of the time it is better for that code or resources not to be loaded at all when the assembly is loaded; in most cases, it just causes an unnecessary performance hit. By separating those parts of the assembly into one or more individual files, you ensure that they are only loaded on demand.

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net