0 out of 12 rated this helpful - Rate this topic

AppDomain.AssemblyResolve Event

Updated: May 2010

Occurs when the resolution of an assembly fails.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
public event ResolveEventHandler AssemblyResolve

Implements

_AppDomain.AssemblyResolve

For this event, the ResolveEventArgs.Name property returns the assembly name before policy is applied.

It is the responsibility of ResolveEventHandler for this event to return the assembly that resolves the type, assembly, or resource. The assembly must be loaded into an execution context; if it is loaded into the reflection-only context, the load that caused this event to be raised fails.

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 example demonstrates the AssemblyResolve 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.

public class MyType
{
    public MyType()
    {
        Console.WriteLine();
        Console.WriteLine("MyType instantiated!");
    }
}

class Test
{
    public static void Main()
    {
        AppDomain currentDomain = AppDomain.CurrentDomain;

        // This call will fail to create an instance of MyType since the 
        // assembly resolver is not set
        InstantiateMyTypeFail(currentDomain);

        currentDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);

        // This call will succeed in creating an instance of MyType since the 
        // assembly resolver is now set.
        InstantiateMyTypeFail(currentDomain);

        // This call will succeed in creating an instance of MyType since the 
        // assembly name is valid.
        InstantiateMyTypeSucceed(currentDomain);
    }

    private static void InstantiateMyTypeFail(AppDomain domain)
    {
        // Calling InstantiateMyType will always fail since the assembly info 
        // given to CreateInstance is invalid. 
        try
        {
            // You must supply a valid fully qualified assembly name here.
            domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyType");
        }
        catch (Exception e)
        {
            Console.WriteLine();
            Console.WriteLine(e.Message);
        }
    }

    private static void InstantiateMyTypeSucceed(AppDomain domain)
    {
        try
        {
            string asmname = Assembly.GetCallingAssembly().FullName;
            domain.CreateInstance(asmname, "MyType");
        }
        catch (Exception e)
        {
            Console.WriteLine();
            Console.WriteLine(e.Message);
        }
    }

    private static Assembly MyResolveEventHandler(object sender, ResolveEventArgs args)
    {
        Console.WriteLine("Resolving...");
        return typeof(MyType).Assembly;
    }
}

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

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

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

Date

History

Reason

May 2010

Clarification: The returned assembly must be loaded into an execution context, not the reflection-only context.

Customer feedback.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.