使用程序集连接器 (Assembly Linker)
除了使用 C# 编译器,你也可以使用 Assembly Linker 工具(AL.exe)来作成包含多个模块(这些模块可以是不同开发语言编译器编译的产物)的程序集。通过这种方法能够生成纯资源程序集 (术名: satellite assemblies),专门用来对应本地化。再次拿前文生成MultiFileLibrary.dll的例子来说明,这次我们改用以下编译命令:
csc /t:module RUT.cs
csc /t:module FUT.cs
al /out: MultiFileLibrary.dll /t:library FUT.netmodule RUT.netmodule
图 2-3 显示了这些命令产生的结果
本例中先生成了2个模块 RUT.netmodule, FUT.netmodule,然后合并成 MultiFileLibrary.dll。这个程序集由3个文件组成: MultiFileLibrary.dll, RUT.netmodule, 以及FUT.netmodule。
通过使用不同的 /t[arget] 参数,AL.exe 工具能够生成 CUI, GUI, 以及 Windows Store app PE 文件。另外在生成可执行文件(EXE)时,你可以通过 /main 命令行来指定哪个模块的哪个函数作为入口(Main)函数。
csc /t:module /r:MultiFileLibrary.dll Program.cs
al /out:Program.exe /t:exe /main:Program.Main Program.netmodule
这个入口函数在IL 中名为 __EntryPoint,如下代码段:
.method privatescope static void __EntryPoint$PST06000001() cil managed
{
.entrypoint
// Code size 8 (0x8)
.maxstack 8
IL_0000: tail.
IL_0002: call void [.module 'Program.netmodule']Program::Main()
IL_0007: ret
} // end of method 'Global Functions'::__EntryPoint
向程序集追加资源文件
使用 AL.exe 的 /embed[resource] 开关能够把一个文件作为资源加入到程序集中(加入时 ManifestResourceDef 表会被更新),该文件的内容会嵌入到PE 文件中。
AL.exe 也支持 /link[resource] 开关,也是把一个包含资源的文件组入进程序集中,但是 /link[resource] 开关更新ManifestResourceDef 以及 FileDef 表,资源文件并不嵌入到程序集的 PE 文件中,它依然保持独立。CSC.exe 也有类似 AL.exe 的功能,对应的命令开关分别为 /resource 和 /linkresource。
最后提一下标准的 Win32 资源:这些资源也能够被AL.exe 或 CSC.exe组入进程序集中,只需要使用 /win32res 命令行开关并传入 .res 文件路径。另外使用 /win32icon 开关能便捷地组入标准 Win32 图标资源。在 Visual Studio 中,打开工程属性的程序选择卡可以追加资源文件。
提示:托管程序集包含有 Win32 manifest 资源信息,缺省情况下 C# 编译器自动生成这些 manifest 信息,但是你可以使用 /nowin32manifest 开关来阻止它。
C# 编译器生成的缺省manifest 信息如下:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity version="1.0.0.0" name="MyApplication.app" />
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
<requestedExecutionLevel level="asInvoker" uiAccess="false"/>
</requestedPrivileges>
</security>
</trustInfo>
</assembly>