다음을 통해 공유


Building a Multifile Assembly

You can create multifile assemblies using command-line compilers or Visual Studio .NET with the Managed Extensions for C++. One file in the assembly must contain the assembly manifest. An assembly that starts an application must also contain an entry point, such as a Main or WinMain method.

For example, suppose you have an application that contains two code modules, Client.cs and Stringer.cs. Stringer.cs creates the myStringer namespace that is referenced by the code in Client.cs. Client.cs contains the Main method, which is the application's entry point. In this example, you compile the two code modules, and then create a third file that contains the assembly manifest, which launches the application. The assembly manifest references both the Client and Stringer modules.

Note   Multifile assemblies can have only one entry point, even if the assembly has multiple code modules.

There are several reasons you might want to create a multifile assembly:

  • To combine modules written in different languages. This is the most common reason for creating a multifile assembly.

  • To optimize downloading an application by putting seldom-used types in a module that is downloaded only when needed.

    Note   If you are creating applications that will be downloaded using the <object> tag with Microsoft Internet Explorer, it is important that you create multifile assemblies. In this scenario, you create a file separate from your code modules that contains only the assembly manifest. Internet Explorer downloads the assembly manifest first, and then creates worker threads to download any additional modules or assemblies required. While the file containing the assembly manifest is being downloaded, Internet Explorer will be unresponsive to user input. The smaller the file containing the assembly manifest, the less time Internet Explorer will be unresponsive.

  • To combine code modules written by several developers. Although each developer can compile each code module into an assembly, this can force some types to be exposed publicly that are not exposed if all modules are put into a multifile assembly.

Once you create the assembly, you can sign the file that contains the assembly manifest (and hence the assembly), or you can give the file (and the assembly) a strong name and put it in the global assembly cache.

To create a multifile assembly

  1. Compile all files that contain namespaces referenced by other modules in the assembly into code modules. The default extension for code modules is .netmodule. For example, if a file called Stringer creates a namespace called myStringer that is referenced in the Client file code, Stringer should be compiled into a code module first.

  2. Compile all other modules, using the necessary compiler options to indicate the other modules that are referenced in the code.

  3. Use the Assembly Linker (Al.exe) to create the output file that contains the assembly manifest. This file contains reference information for all modules or resources that are part of the assembly. The manifest can act as the executable for the application.

    Note   The Visual Studio .NET IDE for C# and Visual Basic can only be used to create single-file assemblies. If you want to create multifile assemblies, you must use the command-line compilers or Visual Studio .NET with the Managed Extensions for C++.

The next section contains a multifile assembly example illustrating those three steps.

See Also

Programming with Assemblies