AppDomain.AssemblyResolve AppDomain.AssemblyResolve AppDomain.AssemblyResolve AppDomain.AssemblyResolve Event

정의

어셈블리를 확인하지 못할 경우 발생합니다.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 'New

End Class 'MyType

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 'Main

    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 'InstantiateMyType

    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 'Test

설명

책임이 합니다 ResolveEventHandler 어셈블리를 반환 하 여 지정 된이 이벤트에 대 한는 ResolveEventArgs.Name 속성 또는 어셈블리 인식 되지 않으면 null을 반환 합니다.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. 운영 체제에 지역화 된 처리기에 여러 번 호출할 수 있습니다: 대체 (fallback) 체인의 각 문화권에 한 번씩입니다.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.

적용 대상

추가 정보