AppDomain.TypeResolve 事件

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

**命名空间:**System
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
Public Event TypeResolve As ResolveEventHandler
用法
Dim instance As AppDomain
Dim handler As ResolveEventHandler

AddHandler instance.TypeResolve, handler
public event ResolveEventHandler TypeResolve
public:
virtual event ResolveEventHandler^ TypeResolve {
    void add (ResolveEventHandler^ value) sealed;
    void remove (ResolveEventHandler^ value) sealed;
}
/** @event */
public final void add_TypeResolve (ResolveEventHandler value)

/** @event */
public final void remove_TypeResolve (ResolveEventHandler value)
JScript 支持使用事件,但不支持进行新的声明。

备注

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

此事件的 ResolveEventHandler 可尝试定位和创建类型。

但是,如果运行库确定在某些程序集中不可能找到类型,则不会发生 TypeResolve 事件。例如,如果在静态程序集中找不到类型,则不会发生此事件,因为运行库知道不能向静态程序集动态地添加类型。

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

有关处理事件的更多信息,请参见 使用事件

示例

下面的示例阐释 TypeResolve 事件。

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

Option Strict On
Option Explicit On

Imports System
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".
'
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".
 */
#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".
 */

.NET Framework 安全性

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

AppDomain 类
AppDomain 成员
System 命名空间