Ilasm.exe(IL 汇编程序)Ilasm.exe (IL Assembler)

IL 汇编程序可利用中间语言 (IL) 生成可移植可执行 (PE) 文件。The IL Assembler generates a portable executable (PE) file from intermediate language (IL). (有关 IL 的详细信息,请参阅托管执行过程。)可以运行生成的可执行文件(包含 IL 和所需的元数据)以确定 IL 是否按预期执行。(For more information on IL, see Managed Execution Process.) You can run the resulting executable, which contains IL and the required metadata, to determine whether the IL performs as expected.

此工具会自动随 Visual Studio 一起安装。This tool is automatically installed with Visual Studio. 若要运行此工具,请使用 Visual Studio 开发人员命令提示(或 Windows 7 中的 Visual Studio 命令提示)。To run the tool, use the Developer Command Prompt for Visual Studio (or the Visual Studio Command Prompt in Windows 7). 有关详细信息,请参阅命令提示For more information, see Command Prompts.

在命令提示符处,键入以下内容:At the command prompt, type the following:

语法Syntax

ilasm [options] filename [[options]filename...]

参数Parameters

参数Argument 说明Description
filename .il 源文件的名称。The name of the .il source file. 该文件包含元数据声明指令和符号化 IL 指令。This file consists of metadata declaration directives and symbolic IL instructions. 可以提供多个源文件参数,以便用 Ilasm.exe 生成单个 PE 文件 。Multiple source file arguments can be supplied to produce a single PE file with Ilasm.exe. 注意: 确保 .il 源文件中的最后一行代码具有尾随空格或行尾字符。Note: Ensure that the last line of code in the .il source file has either trailing white space or an end-of-line character.
选项Option 说明Description
/32bitpreferred/32bitpreferred 创建 32 位首选映像 (PE32+)。Creates a 32-bit-preferred image (PE32).
/alignment: integer/alignment: integer 将 FileAlignment 设置为由 NT Optional 标头中的 integer 指定的值。Sets FileAlignment to the value specified by integer in the NT Optional header. 如果在文件中指定了 .alignment IL 指令,则此选项将重写它。If the .alignment IL directive is specified in the file, this option overrides it.
/appcontainer/appcontainer 生成在 Windows 应用容器中运行的 .dll 或 .exe 文件作为输出 。Produces a .dll or .exe file that runs in the Windows app container, as output.
/arm/arm 指定高级 RISC 计算机 (ARM) 作为目标处理器。Specifies the Advanced RISC Machine (ARM) as the target processor.

如果未指定映像位数,则默认值为 /32bitpreferredIf no image bitness is specified, the default is /32bitpreferred.
/base: integer/base: integer 将 ImageBase 设置为由 NT Optional 标头中的 integer 指定的值。Sets ImageBase to the value specified by integer in the NT Optional header. 如果在文件指定了 .imagebase IL 指令,则此选项将重写它。If the .imagebase IL directive is specified in the file, this option overrides it.
/clock/clock 为指定的 .il 源文件测量并报告下列编译时间(以毫秒为单位):Measures and reports the following compilation times in milliseconds for the specified .il source file:

总运行时间: 执行后面的所有特定操作所花费的总时间。Total Run: The total time spent performing all the specific operations that follow.

启动 :加载并打开文件。Startup: Loading and opening the file.

发出 MD :发出元数据。Emitting MD: Emitting metadata.

定义引用解析 :解析对文件中的定义的引用。Ref to Def Resolution: Resolving references to definitions in the file.

CEE 文件生成 :在内存中生成文件映像。CEE File Generation: Generating the file image in memory.

PE 文件写入 :将映像写入 PE 文件。PE File Writing: Writing the image to a PE file.
/debug[:IMPL|OPT]/debug[:IMPL|OPT] 包括调试信息(局部变量名和参数名以及行号)。Includes debug information (local variable and argument names, and line numbers). 创建 PDB 文件。Creates a PDB file.

不带任何附加值的 /debug 禁用 JIT 优化,并使用 PDB 文件中的序列点。/debug with no additional value disables JIT optimization and uses sequence points from the PDB file.

IMPL 禁用 JIT 优化,并使用隐式序列点。IMPL disables JIT optimization and uses implicit sequence points.

OPT 启用 JIT 优化,并使用隐式序列点。OPT enables JIT optimization and uses implicit sequence points.
/dll/dll 生成 .dll 文件作为输出 。Produces a .dll file as output.
/enc: file/enc: file 从指定的源文件创建“编辑并继续”增量。Creates Edit-and-Continue deltas from the specified source file.

此参数仅可用于教学目的,不支持商业使用。This argument is for academic use only and is not supported for commercial use.
/exe/exe 生成可执行文件作为输出。Produces an executable file as output. 这是默认设置。This is the default.
/flags: integer/flags: integer 将 ImageFlags 设置为由公共语言运行时标头中的 integer 指定的值。Sets ImageFlags to the value specified by integer in the common language runtime header. 如果在文件中指定了 .corflags IL 指令,则此选项将重写它。If the .corflags IL directive is specified in the file, this option overrides it. 有关 integer的有效值的列表,请参见 CorHdr.h 中的 COMIMAGE_FLAGS。See CorHdr.h, COMIMAGE_FLAGS for a list of valid values for integer.
/fold/fold 将相同的方法体合并为一体。Folds identical method bodies into one.
/highentropyva/highentropyva 生成支持高熵地址空间布局随机化 (ASLR) 的可执行输出。Produces an output executable that supports high-entropy address space layout randomization (ASLR). /appcontainer的默认值。)(Default for /appcontainer.)
/include: includePath/include: includePath 设置要在其中搜索 #include包含的文件的路径。Sets a path to search for files included with #include.
/itanium/itanium 指定 Intel 的 Itanium 作为目标处理器。Specifies Intel Itanium as the target processor.

如果未指定映像位数,则默认值为 /pe64If no image bitness is specified, the default is /pe64.
/key: keyFile/key: keyFile 使用 keyFile 中包含的私钥编译具有强签名的 filenameCompiles filename with a strong signature using the private key contained in keyFile.
/key: @keySource/key: @keySource 使用 keySource 中生成的私钥编译具有强签名的 filenameCompiles filename with a strong signature using the private key produced at keySource.
/listing/listing 在标准输出上生成列表文件。Produces a listing file on the standard output. 如果省略此选项,则不生成列表文件。If you omit this option, no listing file is produced.

此参数在 .NET Framework 2.0 或更高版本中不受支持。This parameter is not supported in the .NET Framework 2.0 or later.
/mdv: versionString/mdv: versionString 设置元数据版本字符串。Sets the metadata version string.
/msv: major.minor/msv: major.minor 设置元数据流版本,其中 majorminor 是整数。Sets the metadata stream version, where major and minor are integers.
/noautoinherit/noautoinherit 当未指定基类时,禁用从 Object 的默认继承。Disables default inheritance from Object when no base class is specified.
/nocorstub/nocorstub 取消生成 CORExeMain 存根。Suppresses generation of the CORExeMain stub.
/nologo/nologo 取消显示 Microsoft 启动版权标志。Suppresses the Microsoft startup banner display.
/output: file.ext/output: file.ext 指定输出文件名和扩展名。Specifies the output file name and extension. 默认情况下,输出文件名与第一个源文件的名称相同。By default, the output file name is the same as the name of the first source file. 默认扩展名为 .exe 。The default extension is .exe. 如果指定 /dll 选项,则默认扩展名为 .dll 。If you specify the /dll option, the default extension is .dll. 注意: 指定 /output:myfile.dll 并不会设置 /dll 选项 。Note: Specifying /output:myfile.dll does not set the /dll option. 如果不指定 /dll,结果将会是名为 myfile.dll 的可执行文件 。If you do not specify /dll, the result will be an executable file named myfile.dll.
/optimize/optimize 将长指令优化为短指令。Optimizes long instructions to short. 例如,将 br 优化为 br.sFor example, br to br.s.
/pe64/pe64 创建 64 位映像 (PE32+)。Creates a 64-bit image (PE32+).

如果未指定目标处理器,则默认值为 /itaniumIf no target processor is specified, the default is /itanium.
/pdb/pdb 创建 PDB 文件但不启用调试信息跟踪。Creates a PDB file without enabling debug information tracking.
/quiet/quiet 指定安静模式;不报告程序集进度。Specifies quiet mode; does not report assembly progress.
/resource: file.res/resource: file.res 在生成的 .exe 或 .dll 文件中包括 *.res 格式的指定资源文件 。Includes the specified resource file in *.res format in the resulting .exe or .dll file. 使用 /resource 选项只能指定一个 .res 文件。Only one .res file can be specified with the /resource option.
/ssver: int.int/ssver: int.int 在 NT Optional 标头中设置子系统版本号。Sets the subsystem version number in the NT optional header. 对于 /appcontainer/arm ,最低版本号为 6.02。For /appcontainer and /arm the minimum version number is 6.02.
/stack: stackSize/stack: stackSize 将 NT Optional 标头中的 SizeOfStackReserve 值设置为 stackSizeSets the SizeOfStackReserve value in the NT Optional header to stackSize.
/stripreloc/stripreloc 指定不需要基重定位。Specifies that no base relocations are needed.
/subsystem: integer/subsystem: integer 将 subsystem 设置为由 NT Optional 标头中的 integer 指定的值。Sets subsystem to the value specified by integer in the NT Optional header. 如果在文件中指定了 .subsystem IL 指令,则此命令将重写它。If the .subsystem IL directive is specified in the file, this command overrides it. 有关 integer 的有效值的列表,请参见 winnt.h 中的 IMAGE_SUBSYSTEM。See winnt.h, IMAGE_SUBSYSTEM for a list of valid values for integer.
/x64/x64 指定 64 位 AMD 处理器作为目标处理器。Specifies a 64-bit AMD processor as the target processor.

如果未指定映像位数,则默认值为 /pe64If no image bitness is specified, the default is /pe64.
/?/? 显示该工具的命令语法和选项。Displays command syntax and options for the tool.

备注

Ilasm.exe 的所有选项都不区分大小写,并且根据前三个字母识别 。All options for Ilasm.exe are case-insensitive and recognized by the first three letters. 例如, /lis 等效于 /listing,/res:myresfile.res 等效于 /resource:myresfile.res 。指定参数的选项既可以用冒号 (:) 也可以用等号 (=) 作为选项和参数之间的分隔符。For example, /lis is equivalent to /listing and /res:myresfile.res is equivalent to /resource:myresfile.res. Options that specify arguments accept either a colon (:) or an equal sign (=) as the separator between the option and the argument. 例如,/output:file.ext 等效于 /output=file.ext 。For example, /output:file.ext is equivalent to /output=file.ext.

备注Remarks

IL 汇编程序有助于工具供应商设计和实现 IL 生成器。The IL Assembler helps tool vendors design and implement IL generators. 通过使用 Ilasm.exe,工具和编译器开发人员可以专注于生成 IL 和元数据,而无需考虑如何以 PE 文件格式发出 IL 。Using Ilasm.exe, tool and compiler developers can concentrate on IL and metadata generation without being concerned with emitting IL in the PE file format.

与面向运行时的其他编译器(如 C# 和 Visual Basic)类似,Ilasm.exe 不产生中间对象文件,并且不需要链接阶段即可形成 PE 文件 。Similar to other compilers that target the runtime, such as C# and Visual Basic, Ilasm.exe does not produce intermediate object files and does not require a linking stage to form a PE file.

IL 汇编程序可以展现以运行时为目标的编程语言的所有现有元数据和 IL 功能。The IL Assembler can express all the existing metadata and IL features of the programming languages that target the runtime. 这使得用上面任何编程语言编写的托管代码都可以在 IL 汇编程序中充分展现并且可以用 Ilasm.exe 编译 。This allows managed code written in any of these programming languages to be adequately expressed in IL Assembler and compiled with Ilasm.exe.

备注

如果 .il 源文件中的最后一行代码不具有尾随空格或行尾字符,则编译可能会失败。Compilation might fail if the last line of code in the .il source file does not have either trailing white space or an end-of-line character.

可以将 Ilasm.exe 与它的配套工具 Ildasm.exe一起使用 。You can use Ilasm.exe in conjunction with its companion tool, Ildasm.exe. Ildasm.exe 利用包含 IL 代码的 PE 文件,创建适合输入到 Ilasm.exe 的文本文件 。Ildasm.exe takes a PE file that contains IL code and creates a text file suitable as input to Ilasm.exe. 这很有用,例如在编译用并非支持所有运行时元数据特性的编程语言编写的代码时。This is useful, for example, when compiling code in a programming language that does not support all the runtime metadata attributes. 通过 Ildasm.exe 编译该代码并运行输出后,可以手动编辑生成的 IL 文本文件以添加缺少的属性 。After compiling the code and running the output through Ildasm.exe, the resulting IL text file can be hand-edited to add the missing attributes. 然后可通过 Ilasm.exe 运行此文本文件,生成最终的可执行文件 。You can then run this text file through the Ilasm.exe to produce a final executable file.

也可以使用此方法从最初由不同的编译器生成的数个 PE 文件生成一个 PE 文件。You can also use this technique to produce a single PE file from several PE files originally generated by different compilers.

备注

目前,无法对包含嵌入的本机代码的 PE 文件(例如,由 Visual C++ 生成的 PE 文件)使用此技术。Currently, you cannot use this technique with PE files that contain embedded native code (for example, PE files produced by Visual C++).

为尽可能准确地使用 Ildasm.exe 和 Ilasm.exe 的这种组合,默认情况下汇编程序不会将可能已在 IL 源中写入(或可能由其他编译程序发出)的长编码替换为短编码 。To make this combined use of Ildasm.exe and Ilasm.exe as accurate as possible, by default the assembler does not substitute short encodings for long ones you might have written in your IL sources (or that might be emitted by another compiler). 请尽可能使用 /optimize 选项替代为短编码。Use the /optimize option to substitute short encodings wherever possible.

备注

Ildasm.exe 只对磁盘上的文件进行操作 。Ildasm.exe only operates on files on disk. 它不对安装在全局程序集缓存中的文件进行操作。It does not operate on files installed in the global assembly cache.

有关 IL 语法的更多信息,请参见 Windows SDK 中的 asmparse.grammar 文件。For more information about the grammar of IL, see the asmparse.grammar file in the Windows SDK.

版本信息Version Information

从 .NET Framework 4.5 开始,可以使用类似于下面的代码将自定义特性附加到接口实现:Starting with the .NET Framework 4.5, you can attach a custom attribute to an interface implementation by using code similar to the following:

.class interface public abstract auto ansi IMyInterface
{
  .method public hidebysig newslot abstract virtual
    instance int32 method1() cil managed
  {
  } // end of method IMyInterface::method1
} // end of class IMyInterface
.class public auto ansi beforefieldinit MyClass
  extends [mscorlib]System.Object
  implements IMyInterface
  {
    .interfaceimpl type IMyInterface
    .custom instance void
      [mscorlib]System.Diagnostics.DebuggerNonUserCodeAttribute::.ctor() = ( 01 00 00 00 )
      …

从 .NET Framework 4.5 开始,可以指定任意封送 BLOB(二进制大型对象),方法是使用其原始二进制表示形式,如以下代码所示:Starting with the .NET Framework 4.5, you can specify an arbitrary marshal BLOB (binary large object) by using its raw binary representation, as shown in the following code:

.method public hidebysig abstract virtual
        instance void
        marshal({ 38 01 02 FF })
        Test(object A_1) cil managed

有关 IL 语法的更多信息,请参见 Windows SDK 中的 asmparse.grammar 文件。For more information about the grammar of IL, see the asmparse.grammar file in the Windows SDK.

示例Examples

下面的命令对 IL 文件 myTestFile.il 进行汇编并生成可执行文件 myTestFile.exe 。The following command assembles the IL file myTestFile.il and produces the executable myTestFile.exe.

ilasm myTestFile

下面的命令对 IL 文件 myTestFile.il 进行汇编并生成 .dll 文件 myTestFile.dll 。The following command assembles the IL file myTestFile.il and produces the .dll file myTestFile.dll.

ilasm myTestFile /dll

下面的命令对 IL 文件 myTestFile.il 进行汇编并生成 .dll 文件 myNewTestFile.dll 。The following command assembles the IL file myTestFile.il and produces the .dll file myNewTestFile.dll.

ilasm myTestFile /dll /output:myNewTestFile.dll

下面的代码示例演示了一个向控制台显示“Hello World!”的极其简单的The following code example shows an extremely simple application that displays "Hello World!" “Hello World!”。to the console. 可编译此代码,然后使用 Ildasm.exe 工具生成 IL 文件 。You can compile this code and then use the Ildasm.exe tool to generate an IL file.

using System;

public class Hello
{
    public static void Main(String[] args)
    {
        Console.WriteLine("Hello World!");
    }
}

下面的 IL 代码示例对应于前面的 C# 代码示例。The following IL code example corresponds to the previous C# code example. 可使用 IL Assembler 工具将此代码编译为程序集。You can compile this code into an assembly using the IL Assembler tool. IL 和 C# 代码示例都向控制台显示Both IL and C# code examples display "Hello World!" “Hello World!”。to the console.

// Metadata version: v2.0.50215
.assembly extern mscorlib
{
  .publickeytoken = (B7 7A 5C 56 19 34 E0 89 )                         // .z\V.4..
  .ver 2:0:0:0
}
.assembly sample
{
  .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilationRelaxationsAttribute::.ctor(int32) = ( 01 00 08 00 00 00 00 00 )
  .hash algorithm 0x00008004
  .ver 0:0:0:0
}
.module sample.exe
// MVID: {A224F460-A049-4A03-9E71-80A36DBBBCD3}
.imagebase 0x00400000
.file alignment 0x00000200
.stackreserve 0x00100000
.subsystem 0x0003       // WINDOWS_CUI
.corflags 0x00000001    //  ILONLY
// Image base: 0x02F20000

// =============== CLASS MEMBERS DECLARATION ===================

.class public auto ansi beforefieldinit Hello
       extends [mscorlib]System.Object
{
  .method public hidebysig static void  Main(string[] args) cil managed
  {
    .entrypoint
    // Code size       13 (0xd)
    .maxstack  8
    IL_0000:  nop
    IL_0001:  ldstr      "Hello World!"
    IL_0006:  call       void [mscorlib]System.Console::WriteLine(string)
    IL_000b:  nop
    IL_000c:  ret
  } // end of method Hello::Main

  .method public hidebysig specialname rtspecialname
          instance void  .ctor() cil managed
  {
    // Code size       7 (0x7)
    .maxstack  8
    IL_0000:  ldarg.0
    IL_0001:  call       instance void [mscorlib]System.Object::.ctor()
    IL_0006:  ret
  } // end of method Hello::.ctor

} // end of class Hello

请参阅See also