AppDomain.TypeResolve Event

Occurs when the resolution of a type fails.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

public event ResolveEventHandler TypeResolve
/** @event */
public final void add_TypeResolve (ResolveEventHandler value)

/** @event */
public final void remove_TypeResolve (ResolveEventHandler value)

JScript supports the use of events, but not the declaration of new ones.

The TypeResolve event occurs when the common language runtime is unable to determine the assembly that can create the requested type. 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. The latter situation can occur when Type.GetType is called with a type name that is not qualified with the assembly name.

The ResolveEventHandler for this event can attempt to locate and create the type.

However, the TypeResolve event does not occur if the runtime knows it is not possible to find a type in certain assemblies. 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.

To register an event handler for this event, you must have the required permissions, or a SecurityException is thrown.

For more information about handling events, see Consuming Events.

The following sample demonstrates the TypeResolve 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.

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,
        ModuleBuilder mb = ab.DefineDynamicModule(aName.Name, moduleName);

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

        // First, try to load the type without saving the dynamic 
        // assembly and without hooking up the TypeResolve event. The
        // type cannot be loaded.
            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.
        return Assembly.Load(ab.FullName); 

/* This code example produces the following output:

Loader could not resolve the type.
TypeResolve event handler.
Loaded type "Example".

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

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0