AppDomain.TypeResolve 事件

定义

在对类型的解析失败时发生。

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

事件类型

实现

属性

示例

以下示例演示 了 事件 TypeResolve

若要运行此代码示例,必须提供完全限定的程序集名称。 有关如何获取完全限定程序集名称的信息,请参阅 程序集名称

#using <System.dll>
#using <System.Windows.Forms.dll>
#using <System.Drawing.dll>

using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;

ref class Test
{
private:
    static Assembly^ HandleTypeResolve(Object^ sender, ResolveEventArgs^ args) 
    {
        Console::WriteLine("TypeResolve event handler.");

        // Save the dynamic assembly, and then load it using its
        // display name. Return the loaded assembly.
        //
        ab->Save(moduleName);
        return Assembly::Load(ab->FullName); 
    }

    // For this code example, the following information needs to be
    // available to both Demo and the HandleTypeResolve event
    // handler:
    static AssemblyBuilder^ ab;
    static String^ moduleName;

public:
    static void Demo() 
    {
        AppDomain^ currDom = AppDomain::CurrentDomain;

        // Create a dynamic assembly with one module, to be saved to 
        // disk (AssemblyBuilderAccess::Save).
        // 
        AssemblyName^ aName = gcnew AssemblyName();
        aName->Name = "Transient";
        moduleName = aName->Name + ".dll";
        ab = currDom->DefineDynamicAssembly(aName,
            AssemblyBuilderAccess::Save);
        ModuleBuilder^ mb = ab->DefineDynamicModule(aName->Name, moduleName);

        // The dynamic assembly has just one dummy type, to demonstrate
        // type resolution.
        TypeBuilder^ tb = mb->DefineType("Example");
        tb->CreateType();


        // First, try to load the type without saving the dynamic 
        // assembly and without hooking up the TypeResolve event. The
        // type cannot be loaded.
        try
        {
            Type^ temp = Type::GetType("Example", true);
            Console::WriteLine("Loaded type {0}.", temp);
        }
        catch (TypeLoadException^)
        {
            Console::WriteLine("Loader could not resolve the type.");
        }

        // Hook up the TypeResolve event.
        //      
        currDom->TypeResolve += 
            gcnew ResolveEventHandler(HandleTypeResolve);

        // Now try to load the type again. The TypeResolve event is 
        // raised, the dynamic assembly is saved, and the dummy type is
        // loaded successfully. Display it to the console, and create
        // an instance.
        Type^ t = Type::GetType("Example", true);
        Console::WriteLine("Loaded type \"{0}\".", t);
        Object^ o = Activator::CreateInstance(t);
    }
};

void main()
{
    Test::Demo();
}

/* This code example produces the following output:

Loader could not resolve the type.
TypeResolve event handler.
Loaded type "Example".
 */
using System;
using System.Reflection;
using System.Reflection.Emit;

class Test
{
    // For this code example, the following information needs to be
    // available to both Main and the HandleTypeResolve event
    // handler:
    private static AssemblyBuilder ab;
    private static string moduleName;

    public static void Main()
    {
        AppDomain currDom = AppDomain.CurrentDomain;

        // Create a dynamic assembly with one module, to be saved to
        // disk (AssemblyBuilderAccess.Save).
        //
        AssemblyName aName = new AssemblyName();
        aName.Name = "Transient";
        moduleName = aName.Name + ".dll";
        ab = currDom.DefineDynamicAssembly(aName,
            AssemblyBuilderAccess.Save);
        ModuleBuilder mb = ab.DefineDynamicModule(aName.Name, moduleName);

        // The dynamic assembly has just one dummy type, to demonstrate
        // type resolution.
        TypeBuilder tb = mb.DefineType("Example");
        tb.CreateType();

        // First, try to load the type without saving the dynamic
        // assembly and without hooking up the TypeResolve event. The
        // type cannot be loaded.
        try
        {
            Type temp = Type.GetType("Example", true);
            Console.WriteLine("Loaded type {0}.", temp);
        }
        catch (TypeLoadException)
        {
            Console.WriteLine("Loader could not resolve the type.");
        }

        // Hook up the TypeResolve event.
        //
        currDom.TypeResolve +=
            new ResolveEventHandler(HandleTypeResolve);

        // Now try to load the type again. The TypeResolve event is
        // raised, the dynamic assembly is saved, and the dummy type is
        // loaded successfully. Display it to the console, and create
        // an instance.
        Type t = Type.GetType("Example", true);
        Console.WriteLine("Loaded type \"{0}\".", t);
        Object o = Activator.CreateInstance(t);
    }

    static Assembly HandleTypeResolve(object sender, ResolveEventArgs args)
    {
        Console.WriteLine("TypeResolve event handler.");

        // Save the dynamic assembly, and then load it using its
        // display name. Return the loaded assembly.
        //
        ab.Save(moduleName);
        return Assembly.Load(ab.FullName);
    }
}

/* This code example produces the following output:

Loader could not resolve the type.
TypeResolve event handler.
Loaded type "Example".
 */
open System
open System.Reflection
open System.Reflection.Emit

let currDom = AppDomain.CurrentDomain

// Create a dynamic assembly with one module, to be saved to
// disk (AssemblyBuilderAccess.Save).
//
let aName = AssemblyName()
aName.Name <- "Transient"
let moduleName = aName.Name + ".dll"
let ab = currDom.DefineDynamicAssembly(aName, AssemblyBuilderAccess.Save)

let handleTypeResolve _ _ =
    printfn "TypeResolve event handler."

    // Save the dynamic assembly, and then load it using its
    // display name. Return the loaded assembly.
    ab.Save moduleName
    Assembly.Load ab.FullName

let mb = ab.DefineDynamicModule(aName.Name, moduleName)

// The dynamic assembly has just one dummy type, to demonstrate
// type resolution.
let tb = mb.DefineType "Example"
tb.CreateType() |> ignore

// First, try to load the type without saving the dynamic
// assembly and without hooking up the TypeResolve event. The
// type cannot be loaded.
try
    let temp = Type.GetType("Example", true)
    printfn $"Loaded type {temp}."
with :? TypeLoadException ->
    printfn "Loader could not resolve the type."

// Hook up the TypeResolve event.
//
currDom.add_TypeResolve(ResolveEventHandler handleTypeResolve)

// Now try to load the type again. The TypeResolve event is
// raised, the dynamic assembly is saved, and the dummy type is
// loaded successfully. Display it to the console, and create
// an instance.
let t = Type.GetType("Example", true)
printfn $"Loaded type \"{t}\"."
let o = Activator.CreateInstance t

(* This code example produces the following output:

Loader could not resolve the type.
TypeResolve event handler.
Loaded type "Example".
 *)
Option Strict On
Option Explicit On

Imports System.Reflection
Imports System.Reflection.Emit

Module Test

    ' For this code example, the following information needs to be
    ' available to both Main and the HandleTypeResolve event
    ' handler:
    Private ab As AssemblyBuilder
    Private moduleName As String

    Sub Main() 
    
        Dim currDom As AppDomain = AppDomain.CurrentDomain

        ' Create a dynamic assembly with one module, to be saved to 
        ' disk (AssemblyBuilderAccess.Save).
        ' 
        Dim aName As AssemblyName = new AssemblyName()
        aName.Name = "Transient"
        moduleName = aName.Name + ".dll"
        ab = currDom.DefineDynamicAssembly(aName, _
            AssemblyBuilderAccess.Save)
        Dim mb As ModuleBuilder = _
            ab.DefineDynamicModule(aName.Name, moduleName)

        ' The dynamic assembly has just one dummy type, to demonstrate
        ' type resolution.
        Dim tb As TypeBuilder = mb.DefineType("Example")
        tb.CreateType()


        ' First, try to load the type without saving the dynamic 
        ' assembly and without hooking up the TypeResolve event. The
        ' type cannot be loaded.
        Try
            Dim temp As Type = Type.GetType("Example", true)
            Console.WriteLine("Loaded type {0}.", temp)
        Catch ex As TypeLoadException
            Console.WriteLine("Loader could not resolve the type.")
        End Try

        ' Hook up the TypeResolve event.
        '      
        AddHandler currDom.TypeResolve, AddressOf HandleTypeResolve

        ' Now try to load the type again. The TypeResolve event is 
        ' raised, the dynamic assembly is saved, and the dummy type is
        ' loaded successfully. Display it to the console, and create
        ' an instance.
        Dim t As Type = Type.GetType("Example", true)
        Console.WriteLine("Loaded type ""{0}"".", t)
        Dim o As Object = Activator.CreateInstance(t)
    End Sub

    Private Function HandleTypeResolve(ByVal sender As Object, _
        ByVal e As ResolveEventArgs) As [Assembly]
    
        Console.WriteLine("TypeResolve event handler.")

        ' Save the dynamic assembly, and then load it using its
        ' display name. Return the loaded assembly.
        '
        ab.Save(moduleName)
        Return [Assembly].Load(ab.FullName) 
    End Function
End Module

' This code example produces the following output:
'
'Loader could not resolve the type.
'TypeResolve event handler.
'Loaded type "Example".
'

注解

TypeResolve当公共语言运行时无法确定可以创建所请求类型的程序集时,将发生 该事件。 如果类型是在动态程序集中定义的,或者该类型未在动态程序集中定义,但运行时不知道在哪个程序集中定义了该类型,则可能会发生这种情况。 使用未使用程序集名称限定的类型名称调用 时 Type.GetType ,可能会出现后一种情况。

ResolveEventHandler此事件的 可以尝试查找并创建类型。

但是, TypeResolve 如果运行时知道在某些程序集中找不到类型,则不会发生该事件。 例如,如果在静态程序集中找不到类型,则不会发生此事件,因为运行时知道类型无法动态添加到静态程序集。

从 .NET Framework 4 开始, ResolveEventArgs.RequestingAssembly 属性包含请求类型的程序集。 有关详细信息,请参阅 ResolveEventArgs.RequestingAssembly

若要为此事件注册事件处理程序,必须具有所需的权限,否则 SecurityException 将引发 。

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

适用于

另请参阅