Ilasm.exe (IL 組譯工具)Ilasm.exe (IL Assembler)

IL Assembler 可從中繼語言 (IL) 中產生可攜式執行檔 (PE)The IL Assembler generates a portable executable (PE) file from intermediate language (IL). (如需 IL 的詳細資訊,請參閱Managed 執行程式)。您可以執行產生的可執行檔(其中包含 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 虛設常式 (Stub) 的產生。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. 預設副檔名是 .exeThe default extension is .exe. 如果指定 /dll 選項,則預設副檔名會是 .dllIf 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 指定安靜模式;不報告組譯碼 (Assembly) 程序。Specifies quiet mode; does not report assembly progress.
/resource: file.res/resource: file.res 以 *.res 格式將指定的資源檔包含在產生的 .exe.dll 檔案中。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 選擇標題上的子系統版本號碼。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 選擇性標頭中的 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 將子系統設定為 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. .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.extFor 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. 這樣以這些程式語言中任何一種所撰寫的 Managed 程式碼都能在 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.exeIlasm.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.exeThe following command assembles the IL file myTestFile.il and produces the executable myTestFile.exe.

ilasm myTestFile

下列命令會組譯 IL 檔 myTestFile.il,並產生 .dll檔案 myTestFile.dllThe following command assembles the IL file myTestFile.il and produces the .dll file myTestFile.dll.

ilasm myTestFile /dll

下列命令會組譯 IL 檔 myTestFile.il,並產生 .dll檔案 myNewTestFile.dllThe 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!" 到主控台。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 組譯工具,將這個程式碼編譯為組件。You can compile this code into an assembly using the IL Assembler tool. IL 和 C# 這兩個程式碼範例都將 "Hello World!" 顯示Both IL and C# code examples display "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