HOW TO:建置多檔案組件

這個章節說明建立多檔案組件時所用的程序,並提供完整的範例,說明程序中的每一個步驟。

若要建立多檔案組件

  1. 將所有檔案編譯為程式碼模組,該檔案所包含的命名空間將由組件的其他模組參考。 程式碼模組的預設副檔名為 . netmodule。 例如,如果稱為 Stringer 的檔案建立稱為 myStringer 的命名空間 (在 Client 檔案程式碼中參考),應先將 Stringer 編譯成程式碼模組。

  2. 使用必要的編譯器選項來編譯其他所有模組,以便指出程式碼中所參考的其他模組。

  3. 使用組件連結器 (Al.exe) 來建立輸出檔案,其中含有組件資訊清單。 這個檔案含有所有模組的參考資訊或者是部分組件的資源。

    注意事項注意事項

    Visual Studio 2005 IDE for C# 和 Visual Basic 只能用來建立單一檔案組件。如果您想建立多檔案組件,必須使用命令列編譯器或搭配 Visual C++ 使用 Visual Studio 2005。

下列程式碼範例說明上述程序的步驟 1,即為使用由其他檔案參考的命名空間編譯檔案。 此範例以 Stringer 檔案的一些簡易程式碼開始。 Stringer 擁有 myStringer 命名空間,其中含有 Stringer 類別。 Stringer 類別含有 StringerMethod,可撰寫單行到主控台。

' Assembly building example in the .NET Framework.
Imports System

Namespace myStringer
    Public Class Stringer
        Public Sub StringerMethod()
            System.Console.WriteLine("This is a line from StringerMethod.")
        End Sub
    End Class
End Namespace
// Assembly building example in the .NET Framework.
using System;

namespace myStringer
{
    public class Stringer
    {
        public void StringerMethod()
        {
            System.Console.WriteLine("This is a line from StringerMethod.");
        }
    }
}
// Assembly building example in the .NET Framework.
using namespace System;

namespace myStringer
{
    public ref class Stringer
    {
    public:
        void StringerMethod()
        {
            System::Console::WriteLine("This is a line from StringerMethod.");
        }
    };
}

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

vbc /t:module Stringer.vb
csc /t:module Stringer.cs
cl /clr:pure /LN Stringer.cpp

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

在上述程序的步驟 2,您必須編譯參考其他模組的模組。 這個步驟使用 /addmodule 編譯器選項。 在下列範例中,Client 程式碼模組擁有進入點 Main 方法,該方法會參考步驟 1 建立的 Stringer.dll 模組方法。

下列範例說明 Client 程式碼。

Imports System
Imports myStringer 'The namespace created in Stringer.netmodule.

Class MainClientApp
    ' Static method Main is the entry point method.
    Public Shared Sub Main()
        Dim myStringInstance As New Stringer()
        Console.WriteLine("Client code executes")
        myStringInstance.StringerMethod()
    End Sub
End Class
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");
        myStringInstance.StringerMethod();
    }
}
#using "Stringer.netmodule"

using namespace System;
using namespace myStringer; //The namespace created in Stringer.netmodule.

ref class MainClientApp
{
    // Static method Main is the entry point method.
public:
    static void Main()
    {
        Stringer^ myStringInstance = gcnew Stringer();
        Console::WriteLine("Client code executes");
        myStringInstance->StringerMethod();
    }
};

int main()
{
    MainClientApp::Main();
}

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

vbc /addmodule:Stringer.netmodule /t:module Client.vb
csc /addmodule:Stringer.netmodule /t:module Client.cs
cl /clr:pure /FUStringer.netmodule /LN Client.cpp

請指定 /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
csc /t:module Stringer.cs
csc Client.cs /addmodule:Stringer.netmodule
cl /clr:pure /LN Stringer.cpp
cl /clr:pure Client.cpp /link /ASSEMBLYMODULE:Stringer.netmodule
  • 單一編譯建立雙檔案組件:
vbc /out:Client.exe Client.vb /out:Stringer.netmodule Stringer.vb
csc /out:Client.exe Client.cs /out:Stringer.netmodule Stringer.cs
cl /clr:pure /LN Stringer.cpp
cl /clr:pure Client.cpp /link /ASSEMBLYMODULE:Stringer.netmodule

您可以使用組件連結器 (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.netmodule 和 Stringer.netmodule 的模組,以及僅包含組件中繼資料且稱為 myAssembly.exe, 的可執行檔所組成。 組件的進入點為 MainClientApp 類別的 Main 方法,位於 Client.dll 中。

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

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

請參閱

工作

HOW TO:檢視組件內容

概念

建立組件

執行階段如何找出組件

多檔案組件