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属性,或返回空值,如果无法识别该程序集。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. 如果多个依赖项的版本可用,知道请求的程序集的标识可能很有用或标识正确的版本中,查找依赖项中。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.RequestingAssemblyFor 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.

重要

如果此事件注册了多个事件处理程序,直到事件处理程序返回值不是按顺序调用事件处理程序nullIf 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.

适用于

另请参阅