방법: 애플리케이션 도메인에 어셈블리 로드

애플리케이션 도메인에 어셈블리를 로드하는 방법에는 여러 가지가 있습니다. System.Reflection.Assembly 클래스의 static(Visual Basic에서는 Shared) Load 메서드를 사용하는 것이 좋습니다. 어셈블리를 로드할 수 있는 다른 방법은 다음과 같습니다.

  • Assembly 클래스의 LoadFrom 메서드는 파일 위치가 지정된 경우 어셈블리를 로드합니다. 이 메서드로 어셈블리를 로드하는 경우 다른 로드 컨텍스트가 사용됩니다.

  • ReflectionOnlyLoadReflectionOnlyLoadFrom 메서드는 리플렉션 전용 컨텍스트에 어셈블리를 로드합니다. 이 컨텍스트에 로드된 어셈블리는 검사할 수만 있고 실행할 수 없으므로 다른 플랫폼을 대상으로 하는 어셈블리를 검사할 수 있습니다. 방법: 리플렉션 전용 컨텍스트에 어셈블리 로드를 참조하세요.

참고 항목

리플렉션 전용 컨텍스트는 .NET Framework 버전 2.0의 새로운 기능입니다.

  • AppDomain 클래스의 CreateInstanceCreateInstanceAndUnwrap과 같은 메서드는 애플리케이션 도메인에 어셈블리를 로드할 수 있습니다.

  • Type 클래스의 GetType 메서드는 어셈블리를 로드할 수 있습니다.

  • System.AppDomain 클래스의 Load 메서드는 어셈블리를 로드할 수 있지만 COM 상호 운용성을 위해 주로 사용됩니다. 호출 시 사용된 애플리케이션 도메인 이외의 애플리케이션 도메인에 어셈블리를 로드하는 데 사용하면 안 됩니다.

참고 항목

.NET Framework 버전 2.0부터 런타임은 현재 로드된 런타임보다 높은 버전 번호를 가진 .NET Framework 버전으로 컴파일된 어셈블리를 로드하지 않습니다. 이는 버전 번호의 주 버전 및 부 버전 구성 요소 조합에 적용됩니다.

로드된 어셈블리의 JIT(Just-In-Time) 컴파일된 코드가 애플리케이션 도메인 간에 공유되는 방식을 지정할 수 있습니다. 자세한 내용은 애플리케이션 도메인 및 어셈블리를 참조하세요.

예시

다음 코드는 "example.exe" 또는 "example.dll"이라는 어셈블리를 로드 현재 애플리케이션 도메인에 로드하고, 어셈블리에서 Example 형식을 가져온 다음 해당 형식에 대한 매개 변수가 없는 MethodA 메서드를 가져와서 실행합니다. 로드된 어셈블리에서 정보를 가져오는 방법에 대한 자세한 내용은 형식 동적 로드 및 사용을 참조하세요.

using namespace System;
using namespace System::Reflection;

public ref class Asmload0
{
public:
    static void Main()
    {
        // Use the file name to load the assembly into the current
        // application domain.
        Assembly^ a = Assembly::Load("example");
        // Get the type to use.
        Type^ myType = a->GetType("Example");
        // Get the method to call.
        MethodInfo^ myMethod = myType->GetMethod("MethodA");
        // Create an instance.
        Object^ obj = Activator::CreateInstance(myType);
        // Execute the method.
        myMethod->Invoke(obj, nullptr);
    }
};

int main()
{
    Asmload0::Main();
}
using System;
using System.Reflection;

public class Asmload0
{
    public static void Main()
    {
        // Use the file name to load the assembly into the current
        // application domain.
        Assembly a = Assembly.Load("example");
        // Get the type to use.
        Type myType = a.GetType("Example");
        // Get the method to call.
        MethodInfo myMethod = myType.GetMethod("MethodA");
        // Create an instance.
        object obj = Activator.CreateInstance(myType);
        // Execute the method.
        myMethod.Invoke(obj, null);
    }
}
Imports System.Reflection

Public Class Asmload0
    Public Shared Sub Main()
        ' Use the file name to load the assembly into the current
        ' application domain.
        Dim a As Assembly = Assembly.Load("example")
        ' Get the type to use.
        Dim myType As Type = a.GetType("Example")
        ' Get the method to call.
        Dim myMethod As MethodInfo = myType.GetMethod("MethodA")
        ' Create an instance.
        Dim obj As Object = Activator.CreateInstance(myType)
        ' Execute the method.
        myMethod.Invoke(obj, Nothing)
    End Sub
End Class

참고 항목