AppDomain.TypeResolve AppDomain.TypeResolve AppDomain.TypeResolve AppDomain.TypeResolve Event

Definice

Vyvolá se v případě, že překlad typu se nezdařil.Occurs when the resolution of a type fails.

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

Implementuje

Atributy

Příklady

Následující ukázka demonstruje TypeResolve událost.The following sample demonstrates the TypeResolve event.

Pro spuštění této ukázky kódu je nutné zadat plně kvalifikovaný název.For this code example to run, you must provide the fully qualified assembly name. Informace o tom, jak získat plně kvalifikovaný název sestavení, naleznete v tématu názvy sestavení.For information about how to obtain the fully qualified assembly name, see Assembly Names.

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

Poznámky

K TypeResolve události dojde, když modul CLR (Common Language Runtime) nemůže určit sestavení, které může vytvořit požadovaný typ.The TypeResolve event occurs when the common language runtime is unable to determine the assembly that can create the requested type. K této chybě může dojít, pokud je typ definován v dynamickém sestavení, nebo není definován typ v dynamickém sestavení, ale modul runtime neví, které sestavení daný typ definuje.This can occur if the type is defined in a dynamic assembly, or the type is not defined in a dynamic assembly but the runtime does not know which assembly the type is defined in. K druhé situaci může dojít, Type.GetType když je volána s názvem typu, který není kvalifikován názvem sestavení.The latter situation can occur when Type.GetType is called with a type name that is not qualified with the assembly name.

ResolveEventHandler Pro tuto událost se může pokusit vyhledat a vytvořit typ.The ResolveEventHandler for this event can attempt to locate and create the type.

K TypeResolve události však nedojde, pokud modul runtime ví, že není možné najít typ v určitých sestaveních.However, the TypeResolve event does not occur if the runtime knows it is not possible to find a type in certain assemblies. Například tato událost se neobjeví, pokud typ nebyl nalezen ve statickém sestavení, protože typy v modulu runtime nemohou být do statických sestavení přidány dynamicky.For example, this event does not occur if the type is not found in a static assembly because the runtime knows types cannot be added dynamically to static assemblies.

.NET Framework 4.NET Framework 4 PočínajeResolveEventArgs.RequestingAssembly , vlastnost obsahuje sestavení, které požadoval typ.Beginning with the .NET Framework 4.NET Framework 4, the ResolveEventArgs.RequestingAssembly property contains the assembly that requested the type. Další informace naleznete v tématu ResolveEventArgs.RequestingAssembly.For more information, see ResolveEventArgs.RequestingAssembly.

Chcete-li zaregistrovat obslužnou rutinu události pro tuto událost, musíte mít požadovaná oprávnění, nebo SecurityException je vyvolána výjimka.To register an event handler for this event, you must have the required permissions, or a SecurityException is thrown.

Další informace o zpracování událostí naleznete v tématu manipulace a vyvolávání událostí.For more information about handling events, see Handling and Raising Events.

Zabezpečení

SecurityCriticalAttribute
vyžaduje úplný vztah důvěryhodnosti pro bezprostředně volajícího.requires full trust for the immediate caller. Tohoto člena nelze použít v kódu s částečnou důvěryhodností ani v transparentním kódu.This member cannot be used by partially trusted or transparent code.

Platí pro

Viz také