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

注解

此事件负责 ResolveEventHandler 返回由 ResolveEventArgs.Name 属性指定的程序集,如果无法识别程序集,则返回 null。 程序集必须加载到执行上下文中;如果它加载到仅反射上下文中,则导致引发此事件的负载将失败。

有关使用此事件的指南,请参阅 解析程序集加载

从 .NET Framework 4 开始,ResolveEventArgs.RequestingAssembly属性返回请求无法解析的程序集加载的程序集。 例如,加载程序可能无法加载请求程序集的依赖项,因为请求程序集及其依赖项不在探测路径中。 如果有多个版本的依赖项可用,则了解请求程序集的标识对于查找依赖项或识别正确版本可能很有用。 有关详细信息,请参阅 ResolveEventArgs.RequestingAssembly

重要

从 .NET Framework 4 开始,ResolveEventHandler为所有程序集(包括资源程序集)引发 事件。 在早期版本中,不会为资源程序集引发 事件。 如果操作系统已本地化,可能会多次调用处理程序:针对回退链中的每个区域性调用一次。

对于此事件, ResolveEventArgs.Name 属性在应用策略之前返回程序集名称。

重要

如果为此事件注册了多个事件处理程序,则会按顺序调用事件处理程序,直到事件处理程序返回的值不是 null。 忽略后续事件处理程序。

有关处理事件的详细信息,请参阅 处理和引发事件

适用于

另请参阅