Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
System Namespace
AppDomain Class
AppDomain Events
 AssemblyResolve Event
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
AppDomain..::.AssemblyResolve Event

Updated: November 2007

Occurs when the resolution of an assembly fails.

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

Visual Basic (Declaration)
Public Event AssemblyResolve As ResolveEventHandler
Visual Basic (Usage)
Dim instance As AppDomain
Dim handler As ResolveEventHandler

AddHandler instance.AssemblyResolve, handler
C#
public event ResolveEventHandler AssemblyResolve
Visual C++
public:
virtual  event ResolveEventHandler^ AssemblyResolve {
    void add (ResolveEventHandler^ value);
    void remove (ResolveEventHandler^ value);
}
J#
/** @event */
public final void add_AssemblyResolve (ResolveEventHandler value)
/** @event */
public final void remove_AssemblyResolve (ResolveEventHandler value)
JScript
JScript does not support events.

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.

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 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.

Visual Basic
Public Class MyType

   Public Sub New()
      Console.WriteLine("MyType instantiated!")
   End Sub 'New

End Class 'MyType

Module Test

   Sub Main()
      Dim currentDomain As AppDomain = AppDomain.CurrentDomain

      InstantiateMyType(currentDomain)    ' Failed!

      AddHandler currentDomain.AssemblyResolve, AddressOf MyResolveEventHandler

      InstantiateMyType(currentDomain)    ' OK!
   End Sub 'Main

   Sub InstantiateMyType(domain As AppDomain)
      Try
     ' You must supply a valid fully qualified assembly name here.
         domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyType")
      Catch e As Exception
         Console.WriteLine(e.Message)
      End Try
   End Sub 'InstantiateMyType

   Function MyResolveEventHandler(sender As Object, args As ResolveEventArgs) As [Assembly]
      Console.WriteLine("Resolving...")
      Return GetType(MyType).Assembly
   End Function 'MyResolveEventHandler

End Module 'Test

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

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

      InstantiateMyType(currentDomain);    // Failed!

      currentDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);

      InstantiateMyType(currentDomain);    // OK!
   }

   static void InstantiateMyType(AppDomain domain) {
      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(e.Message);
      }
   }

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

Visual C++
public ref class MyType
{
public:
   MyType()
   {
      Console::WriteLine( "MyType instantiated!" );
   }

};

ref class Test
{
public:
   static void InstantiateMyType( AppDomain^ domain )
   {
      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( e->Message );
      }
   }

   static Assembly^ MyResolveEventHandler( Object^ sender, ResolveEventArgs^ args )
   {
      Console::WriteLine( "Resolving..." );
      return MyType::typeid->Assembly;
   }
};

int main()
{
   AppDomain^ currentDomain = AppDomain::CurrentDomain;
   Test::InstantiateMyType( currentDomain ); // Failed!
   currentDomain->AssemblyResolve += gcnew ResolveEventHandler( Test::MyResolveEventHandler );
   Test::InstantiateMyType( currentDomain ); // OK!
}

Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, 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
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
See my 'RemoteAppDeployment' article on www.codeproject.com      Indrajeet Valera   |   Edit   |  
For a fully working & reusable example of AssemblyResolve in C#, see my article on www.codeproject.com:

Deploy .NET thick client application assemblies to remote server
By Indrajeet Valera

http://www.codeproject.com/KB/miscctrl/RemoteAppDeployment.aspx

Uses .NET Remoting, AppDomain.CurrentDomain.Load as well.

Enjoy,
Indar
Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker