다음을 통해 공유


방법: 다중 파일 어셈블리 빌드

이 단원에서는 다중 파일 어셈블리를 만드는 절차를 설명하고 절차의 각 단계를 보여 주는 완성된 예제를 제공합니다.

다중 파일 어셈블리를 만들려면

  1. 어셈블리의 다른 모듈에서 참조되는 네임스페이스가 모든 파일에 포함된 경우, 이 파일을 모두 코드 모듈로 컴파일합니다. 코드 모듈의 기본 확장명은 . netmodule입니다. 예를 들어, Client 파일 코드에서 참조되는 myStringer라는 네임스페이스를 Stringer라는 파일에서 만드는 경우, Stringer를 코드 모듈로 먼저 컴파일해야 합니다.

  2. 필요한 컴파일러 옵션을 사용하여 다른 모든 모듈을 컴파일합니다. 이 옵션은 코드에서 참조되는 다른 모듈을 표시합니다.

  3. 어셈블리 링커(Al.exe)를 사용하면 어셈블리 매니페스트가 포함된 출력 파일을 만들 수 있습니다. 이 파일에는 어셈블리의 일부인 모든 모듈과 리소스의 참조 정보가 들어 있습니다.

    참고참고

    C# 및 Visual Basic용 Visual Studio 2005 IDE는 단일 파일 어셈블리를 만드는 경우에만 사용할 수 있습니다.다중 파일 어셈블리를 만들려면 명령줄 컴파일러나 Visual C++의 Visual Studio 2005를 사용해야 합니다.

다음 예제에서는 위 절차의 1단계를 보여 주며 다른 파일에서 참조하는 네임스페이스를 사용하여 파일을 컴파일합니다. 이 예제는 Stringer 파일의 간단한 코드를 사용하여 시작합니다. Stringer 파일에는 Stringer라는 클래스와 myStringer라는 네임스페이스가 있습니다. 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 옵션을 지정합니다. Client 코드는 Stringer.netmodule 코드에서 만들어진 네임스페이스를 참조하므로 /addmodule 옵션을 지정합니다. 컴파일러는 Client.netmodule이라는 모듈을 만드는데, 이 모듈에는 다른 모듈인 Stringer.netmodule에 대한 참조가 들어 있습니다.

참고참고

C# 및 Visual Basic 컴파일러에서는 다음과 같은 두 구문을 사용하여 다중 파일 어셈블리를 직접 만들 수 있습니다.

  • 두 번 컴파일에서 2파일 어셈블리를 만듭니다.
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
  • 한 번 컴파일에서 2파일 어셈블리를 만듭니다.
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: 옵션은 출력 파일의 이름을 지정하는데, 이 파일에는 어셈블리 메타데이터가 들어 있습니다. /target: 옵션은 어셈블리를 콘솔 응용 프로그램 실행 파일(.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)를 사용하면 어셈블리의 콘텐츠를 검사 할 수 있으며, 파일이 어셈블리인지 모듈인지를 결정 할 수 있습니다.

참고 항목

작업

방법: 어셈블리 내용 보기

개념

어셈블리 만들기

런타임에서 어셈블리를 찾는 방법

다중 파일 어셈블리