Share via


多檔案組件範例

本章節提供完整的範例,說明建立多檔案組件的必要步驟。

步驟 1 — 以其他檔案參考的命名空間編譯檔案

該範例使用某些簡易的程式碼做為 Stringer 的開頭,因為 Stringer 擁有 myStringer 命名空間,其中含有 Stringer 類別。Stringer 類別含有 StringerMethod,可撰寫單行到主控台。

' Assembly building example in the .NET Framework SDK.
Imports System
Namespace myStringer 
   Public Class Stringer
      Public Sub StringerMethod() 
         Console.WriteLine("This is a line from StringerMethod.")
      End Sub
   End Class
End Namespace
[C#]
// Assembly building example in the .NET Framework SDK.
using System;
namespace myStringer 
   {
   public class Stringer
      { 
      public void StringerMethod() 
         {
         System.Console.WriteLine("This is a line from StringerMethod.");
         }
      }
   }

請使用下列命令來編譯此程式碼:

vbc /t:module Stringer.vb
[C#]
csc /t:module Stringer.cs

指定 module 參數時使用 /t: 編譯器選項,表示應將檔案編譯成模組,而非編譯成組件。編譯器可產生 Stringer.netmodule 模組,您可以將它新增到組件。

步驟 2 — 以其他模組的參考編譯模組

這個步驟使用 /addmodule 編譯器選項。在這個範例中,Client 程式碼模組擁有進入點 Main 方法,該方法參考步驟 1 中建立的 Stringer.dll 模組方法。

下列範例說明 Client 程式碼。

Imports System
Imports myStringer  'The namespace created in Stringer.netmodule.
Class MainClientApp
   ' Shared method Main is the entry point method.
   Public Shared Sub Main()
      Dim myStringInstance As New Stringer()
      Console.WriteLine("Client code executes")
      'myStringComp.Stringer()
      myStringInstance.StringerMethod()
   End Sub
End Class
[C#]
using System;
using myStringer; //The namespace created in Stringer.netmodule.
class MainClientApp 
{
   // Static method Main is the entry point method.
   public static void Main() 
   { 
      Stringer myStringInstance = new Stringer();
      Console.WriteLine("Client code executes");
      //myStringComp.Stringer();
      myStringInstance.StringerMethod();
   }
}

請使用下列命令來編譯此程式碼:

vbc /addmodule:Stringer.netmodule /t:module Client.vb
[C#]
csc /addmodule:Stringer.netmodule /t:module Client.cs

請指定 /t:module 選項,因為這個模組將在下一個步驟中新增到組件。請指定 /addmodule 選項,因為 Client 的程式碼參考 Stringer.netmodule 程式碼所建立的命名空間。編譯器會產生 Client.netmodule 模組,其中含有對另一個模組 Stringer.netmodule 的參考。

**注意   **C# 和 Visual Basic 編譯器支援使用下列兩個不同的語法來直接建立多檔案組件。

  • 兩種編譯 (Compilation) 建立雙檔案組件:

    vbc /t:module Stringer.vb
    vbc Client.vb /addmodule:Stringer.netmodule
    [C#]
    csc /t:module Stringer.cs
    csc Client.cs /addmodule:Stringer.netmodule
    
  • 單一編譯建立雙檔案組件:

    vbc /out:Stringer.netmodule Stringer.vb /out:Client.exe Client.vb
    [C#]
    csc /out:Client.exe Client.cs /out:Stringer.netmodule Stringer.cs
    

步驟 3 — 使用組件連結器建立多檔案組件

您可以使用組件連結器 (Al.exe) 從編譯程式碼模組集合建立組件。

若要使用組件連結器建立多檔案組件

  • 請在命令提示字元中鍵入下列命令:

    al <module name> <module name> ... /main:<method name> /out:<file name> /target:<assembly file type>

    在這個命令中,module name 引數指定組件中包含所有模組名稱。/main: 選項指定方法名稱,這個名稱是組件的進入點。/out: 選項指定輸出檔案的名稱,其中包含組件中繼資料 (Metadata)。/target: 選項指定組件為主控台應用程式 (Console Application) 可執行檔 (.exe)、Windows 可執行檔 (.win) 或程式庫 (.lib) 檔案。

下列範例中,Al.exe 建立組件,該組件為 myAssembly.exe 主控台應用程式可執行檔。該應用程式是由兩個模組 Client.netmoduleStringer.netmodule,以及 myAssembly.exe 可執行檔後者僅包含組件中繼資料。組件的進入點為 MainClientApp 類別的 Main 方法,位於 Client.dll 中。

al Client.netmodule Stringer.netmodule /main:MainClientApp.Main /out:myAssembly.exe /target:exe 

您可以使用 MSIL 反組譯工具 (Ildasm.exe) 來檢查組件的內容,或判斷檔案是否為組件或模組。

請參閱

建立組件 | 檢視組件內容 | Runtime 如何找出組件 | 建置多檔案組件