AppDomain.AssemblyResolve 이벤트

정의

어셈블리를 확인하지 못할 경우 발생합니다.Occurs when the resolution of an assembly fails.

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

구현

특성

예제

다음 샘플에서는 AssemblyResolve 이벤트를 보여 줍니다.The following sample demonstrates the AssemblyResolve event.

이 코드 예제를 실행 하려면 정규화 된 어셈블리 이름을 제공 해야 합니다.For this code example to run, you must provide the fully qualified assembly name. 참조 된 정규화 된 어셈블리 이름을 가져오는 방법에 대 한 내용은 어셈블리 이름합니다.For information about how to obtain the fully qualified assembly name, see Assembly Names.

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 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 += 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;
    }
}
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의 책임입니다.It is the responsibility of the ResolveEventHandler for this event to return the assembly that is specified by the ResolveEventArgs.Name property, or to return null if the assembly is not recognized. 어셈블리는 실행 컨텍스트에 로드 해야 합니다. 리플렉션 전용 컨텍스트에 로드 되 면이 이벤트를 발생 시킨 부하가 실패 합니다.The assembly must be loaded into an execution context; if it is loaded into the reflection-only context, the load that caused this event to be raised fails.

이 이벤트의 사용에 대 한 지침은 어셈블리 로드 확인을 참조 하세요.For guidance on the use of this event, see Resolving Assembly Loads.

.NET Framework 4.NET Framework 4부터 ResolveEventArgs.RequestingAssembly 속성은 확인할 수 없는 어셈블리 로드를 요청한 어셈블리를 반환 합니다.Beginning with the .NET Framework 4.NET Framework 4, the ResolveEventArgs.RequestingAssembly property returns the assembly that requested the assembly load that could not be resolved. 예를 들어 요청 하는 어셈블리와 해당 종속성이 검색 경로에 없으므로 로더에서 요청 하는 어셈블리의 종속성을 로드 하지 못할 수 있습니다.For example, the loader might be unable to load a dependency of the requesting assembly because the requesting assembly and its dependency are not in the probing path. 사용 가능한 종속성 버전이 두 개 이상 있는 경우 요청 하는 어셈블리의 id를 알면 종속성을 찾거나 올바른 버전을 식별 하는 데 유용할 수 있습니다.Knowing the identity of the requesting assembly might be useful in locating the dependency or in identifying the correct version, if more than one version of the dependency is available. 자세한 내용은 ResolveEventArgs.RequestingAssembly을 참조하세요.For more information, see ResolveEventArgs.RequestingAssembly.

중요

.NET Framework 4.NET Framework 4부터 리소스 어셈블리를 비롯 하 여 모든 어셈블리에 대해 ResolveEventHandler 이벤트가 발생 합니다.Beginning with the .NET Framework 4.NET Framework 4, the ResolveEventHandler event is raised for all assemblies, including resource assemblies. 이전 버전에서는 리소스 어셈블리에 대해 이벤트가 발생 하지 않았습니다.In earlier versions, the event was not raised for resource assemblies. 운영 체제가 지역화 된 경우 처리기는 대체 체인의 각 문화권에 대해 한 번씩, 여러 번 호출 될 수 있습니다.If the operating system is localized, the handler might be called multiple times: once for each culture in the fallback chain.

이 이벤트의 경우 ResolveEventArgs.Name 속성은 정책을 적용 하기 전에 어셈블리 이름을 반환 합니다.For this event, the ResolveEventArgs.Name property returns the assembly name before policy is applied.

중요

이 이벤트에 대해 둘 이상의 이벤트 처리기를 등록 한 경우 이벤트 처리기가 null없는 값을 반환할 때까지 순서 대로 이벤트 처리기가 호출 됩니다.If more than one event handler is registered for this event, the event handlers are called in order until an event handler returns a value that isn't null. 이후 이벤트 처리기는 무시 됩니다.Subsequent event handlers are ignored.

이벤트 처리에 대한 자세한 내용은 이벤트 처리 및 발생 을 참조하십시오.For more information about handling events, see Handling and Raising Events.

보안

SecurityCriticalAttribute
직접 실행 호출자에 대 한 완전 신뢰가 필요 합니다.Requires full trust for the immediate caller. 이 멤버는 부분적으로 신뢰할 수 있는 또는 투명 코드에서 사용할 수 없습니다.This member cannot be used by partially trusted or transparent code.

적용 대상

추가 정보