Building and Consuming a Multifile Assembly

Now that you have constructed and consumed a single-file assembly, let's examine the process of building a multifile assembly. Recall that a multifile assembly is simply a collection of related modules (which has nothing to do with the Visual Basic 2008 Module keyword!) that are deployed and versioned as a single logical unit. At the time of this writing, Visual Studio 2008 does not support a VB 2008 multifile assembly project template. Therefore, you will need to make use of the command-line compiler (vbc.exe) if you wish to build such a beast (see Chapter 2 for details of the command-line compiler).

To illustrate the process, you will build a multifile assembly named AirVehicles. The primary module (airvehicles.dll) will contain a single class type named Helicopter.The related manifest (also contained in airvehicles.dll) catalogs an additional *.netmodule file named ufo.netmodule, which contains another class type named (of course) Ufo. Although both class types are physically contained in separate binaries, you will group them into a single namespace named AirVehicles. Finally, both classes are created using VB 2008 (although you could certainly mix and match languages if you desire).

To begin, open a simple text editor (such as Notepad) and create the following Ufo class definition saved to a file named ufo.vb:

' This type will be placed ' within a *.netmodule binary, ' and is thus part of a multifile ' Assembly.

Namespace AirVehicles Public Class Ufo

Public Sub AbductHumanQ

Console.WriteLine("Resistance is futile") End Sub End Class End Namespace

To compile this class into a .NET module, switch back to the Visual Studio 2008 command prompt, navigate to the folder containing ufo.vb, and issue the following command to the VB 2008 compiler (the module option of the /target flag instructs vbc.exe to produce a *.netmodule as opposed to a *.dll or an *.exe file):

vbc.exe /t:module ufo.vb

If you now look in the folder that contains the ufo.vb file, you should see a new file named ufo.netmodule (take a peek). Next, create a new file named helicopter.vb that contains the following class definition:

' This type will be in the ' primary module of the multifile ' assembly, therefore this assembly ' will contain the assembly manifest.

Namespace AirVehicles Public Class Helicopter Public Sub TakeOff()

Console.WriteLine("Helicopter taking off!") End Sub End Class End Namespace

Given that airvehicles.dll is the intended name of the primary module of this multifile assembly, you will need to compile helicopter.vb using the /t:library and /out: options. To enlist the ufo.netmodule binary into the assembly manifest, you must also specify the /addmodule flag. The following command does the trick:

vbc /t:library /addmodule:ufo.netmodule /out:airvehicles.dll helicopter.vb

At this point, your directory should contain the primary airvehicles.dll module as well as the secondary ufo.netmodule binary.

0 0

Post a comment

  • Receive news updates via email from this site