다음을 통해 공유


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이 throw됩니다.

이벤트 처리에 대한 자세한 내용은 이벤트 사용을 참조하십시오.

예제

다음 샘플에서는 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 네임스페이스