다음을 통해 공유


AppDomain.AssemblyResolve 이벤트

정의

어셈블리를 확인하지 못할 경우 발생합니다.

public:
 event ResolveEventHandler ^ AssemblyResolve;
public:
 virtual event ResolveEventHandler ^ AssemblyResolve;
public event ResolveEventHandler? AssemblyResolve;
public event ResolveEventHandler AssemblyResolve;
[add: System.Security.SecurityCritical]
[remove: System.Security.SecurityCritical]
public event ResolveEventHandler AssemblyResolve;
member this.AssemblyResolve : ResolveEventHandler 
[<add: System.Security.SecurityCritical>]
[<remove: System.Security.SecurityCritical>]
member this.AssemblyResolve : ResolveEventHandler 
Public Custom Event AssemblyResolve As ResolveEventHandler 

이벤트 유형

구현

특성

예제

다음 샘플에서는 이벤트를 보여 줍니다 AssemblyResolve .

이 코드 예제를 실행 하려면 정규화 된 어셈블리 이름을 제공 해야 합니다. 참조 된 정규화 된 어셈블리 이름을 가져오는 방법에 대 한 내용은 어셈블리 이름합니다.

public ref class MyType
{
public:
    MyType()
    {
        Console::WriteLine();
        Console::WriteLine("MyType instantiated!");
    }
};

class Test
{
public:
    static void Main()
    {
        AppDomain^ currentDomain = AppDomain::CurrentDomain;

        // This call will fail to create an instance of MyType since the
        // assembly resolver is not set
        InstantiateMyTypeFail(currentDomain);

        currentDomain->AssemblyResolve += gcnew ResolveEventHandler(&Test::MyResolveEventHandler);

        // This call will succeed in creating an instance of MyType since the
        // assembly resolver is now set.
        InstantiateMyTypeFail(currentDomain);

        // This call will succeed in creating an instance of MyType since the
        // assembly name is valid.
        InstantiateMyTypeSucceed(currentDomain);
    }

private:
    static void InstantiateMyTypeFail(AppDomain^ domain)
    {
        // Calling InstantiateMyType will always fail since the assembly info
        // given to CreateInstance is invalid.
        try
        {
            // You must supply a valid fully qualified assembly name here.
            domain->CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyType");
        }
        catch (Exception^ e)
        {
            Console::WriteLine();
            Console::WriteLine(e->Message);
        }
    }

    static void InstantiateMyTypeSucceed(AppDomain^ domain)
    {
        try
        {
            String^ asmname = Assembly::GetCallingAssembly()->FullName;
            domain->CreateInstance(asmname, "MyType");
        }
        catch (Exception^ e)
        {
            Console::WriteLine();
            Console::WriteLine(e->Message);
        }
    }

    static Assembly^ MyResolveEventHandler(Object^ sender, ResolveEventArgs^ args)
    {
        Console::WriteLine("Resolving...");
        return MyType::typeid->Assembly;
    }
};

int main()
{
    Test::Main();
}
public class MyType
{
    public MyType()
    {
        Console.WriteLine();
        Console.WriteLine("MyType instantiated!");
    }
}

class AssemblyResolveSnippet
{
    public static void Main()
    {
        AppDomain currentDomain = AppDomain.CurrentDomain;

        // This call will fail to create an instance of MyType since the
        // assembly resolver is not set
        InstantiateMyTypeFail(currentDomain);

        currentDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);

        // This call will succeed in creating an instance of MyType since the
        // assembly resolver is now set.
        InstantiateMyTypeFail(currentDomain);

        // This call will succeed in creating an instance of MyType since the
        // assembly name is valid.
        InstantiateMyTypeSucceed(currentDomain);
    }

    private static void InstantiateMyTypeFail(AppDomain domain)
    {
        // Calling InstantiateMyType will always fail since the assembly info
        // given to CreateInstance is invalid.
        try
        {
            // You must supply a valid fully qualified assembly name here.
            domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyType");
        }
        catch (Exception e)
        {
            Console.WriteLine();
            Console.WriteLine(e.Message);
        }
    }

    private static void InstantiateMyTypeSucceed(AppDomain domain)
    {
        try
        {
            string asmname = Assembly.GetCallingAssembly().FullName;
            domain.CreateInstance(asmname, "MyType");
        }
        catch (Exception e)
        {
            Console.WriteLine();
            Console.WriteLine(e.Message);
        }
    }

    private static Assembly MyResolveEventHandler(object sender, ResolveEventArgs args)
    {
        Console.WriteLine("Resolving...");
        return typeof(MyType).Assembly;
    }
}
type MyType() =
    do
        printfn "\nMyType instantiated!"

let instantiateMyTypeFail (domain: AppDomain) =
    // Calling InstantiateMyType will always fail since the assembly info
    // given to CreateInstance is invalid.
    try
        // You must supply a valid fully qualified assembly name here.
        domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyType")
        |> ignore
    with e ->
        printfn $"\n{e.Message}"

let instantiateMyTypeSucceed (domain: AppDomain) =
    try
        let asmname = Assembly.GetCallingAssembly().FullName
        domain.CreateInstance(asmname, "MyType")
        |> ignore
    with e ->
        printfn $"\n{e.Message}"

let myResolveEventHandler _ _ =
    printfn "Resolving..."
    typeof<MyType>.Assembly


let currentDomain = AppDomain.CurrentDomain

// This call will fail to create an instance of MyType since the
// assembly resolver is not set
instantiateMyTypeFail currentDomain

currentDomain.add_AssemblyResolve (ResolveEventHandler myResolveEventHandler)

// This call will succeed in creating an instance of MyType since the
// assembly resolver is now set.
instantiateMyTypeFail currentDomain

// This call will succeed in creating an instance of MyType since the
// assembly name is valid.
instantiateMyTypeSucceed currentDomain
Public Class MyType

    Public Sub New()
        Console.WriteLine()
        Console.WriteLine("MyType instantiated!")
    End Sub

End Class

Class Test

    Public Shared Sub Main()
        Dim currentDomain As AppDomain = AppDomain.CurrentDomain

        ' This call will fail to create an instance of MyType since the
        ' assembly resolver is not set
        InstantiateMyTypeFail(currentDomain)

        AddHandler currentDomain.AssemblyResolve, AddressOf MyResolveEventHandler

        ' This call will succeed in creating an instance of MyType since the
        ' assembly resolver is now set.
        InstantiateMyTypeFail(currentDomain)

        ' This call will succeed in creating an instance of MyType since the
        ' assembly name is valid.
        InstantiateMyTypeSucceed(currentDomain)
    End Sub

    Private Shared Sub InstantiateMyTypeFail(domain As AppDomain)
        ' Calling InstantiateMyType will always fail since the assembly info
        ' given to CreateInstance is invalid.
        Try
            ' You must supply a valid fully qualified assembly name here.
            domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyType")
        Catch e As Exception
            Console.WriteLine()
            Console.WriteLine(e.Message)
        End Try
    End Sub

    Private Shared Sub InstantiateMyTypeSucceed(domain As AppDomain)
        Try
            Dim asmname As String = Assembly.GetCallingAssembly().FullName
            domain.CreateInstance(asmname, "MyType")
        Catch e As Exception
            Console.WriteLine()
            Console.WriteLine(e.Message)
        End Try
    End Sub

    Private Shared Function MyResolveEventHandler(sender As Object, args As ResolveEventArgs) As Assembly
        Console.WriteLine("Resolving...")
        Return GetType(MyType).Assembly
    End Function 'MyResolveEventHandler

End Class

설명

이 이벤트가 속성에 지정된 ResolveEventArgs.Name 어셈블리를 반환하거나 어셈블리가 인식되지 않는 경우 null을 반환하는 것은 의 책임 ResolveEventHandler 입니다. 어셈블리를 실행 컨텍스트로 로드해야 합니다. 리플렉션 전용 컨텍스트에 로드되면 이 이벤트를 발생시킨 로드가 실패합니다.

이 이벤트의 사용에 대한 지침은 어셈블리 로드 해결을 참조하세요.

.NET Framework 4부터 속성은 ResolveEventArgs.RequestingAssembly 확인할 수 없는 어셈블리 로드를 요청한 어셈블리를 반환합니다. 예를 들어 요청 어셈블리와 해당 종속성이 검색 경로에 없기 때문에 로더가 요청 어셈블리의 종속성을 로드하지 못할 수 있습니다. 둘 이상의 종속성을 사용할 수 있는 경우 요청 어셈블리의 ID를 아는 것은 종속성을 찾거나 올바른 버전을 식별하는 데 유용할 수 있습니다. 자세한 내용은 ResolveEventArgs.RequestingAssembly를 참조하세요.

중요

.NET Framework 4 ResolveEventHandler 부터는 리소스 어셈블리를 비롯한 모든 어셈블리에 대해 이벤트가 발생합니다. 이전 버전에서는 리소스 어셈블리에 대해 이벤트가 발생하지 않았습니다. 운영 체제가 지역화된 경우 처리기는 대체 체인의 각 문화권에 대해 한 번씩 여러 번 호출될 수 있습니다.

이 이벤트의 경우 속성은 ResolveEventArgs.Name 정책이 적용되기 전에 어셈블리 이름을 반환합니다.

중요

이 이벤트에 대해 둘 이상의 이벤트 처리기가 등록된 경우 이벤트 처리기가 가 아닌 값을 반환할 때까지 이벤트 처리기가 순서대로 호출됩니다 null. 후속 이벤트 처리기는 무시됩니다.

이벤트 처리에 대한 자세한 내용은 이벤트 처리 및 발생 을 참조하십시오.

적용 대상

추가 정보