Export (0) Print
Expand All

AppDomain.AssemblyResolve Event

Occurs when the resolution of an assembly fails.

[Visual Basic]
Public Overridable Event AssemblyResolve As ResolveEventHandler
[C#]
public virtual event ResolveEventHandler AssemblyResolve;
[C++]
public: virtual __event ResolveEventHandler* AssemblyResolve;

[JScript] In JScript, you can handle the events defined by a class, but you cannot define your own.

Event Data

The event handler receives an argument of type ResolveEventArgs containing data related to this event. The following ResolveEventArgs property provides information specific to this event.

Property Description
Name Gets the name of the item to resolve.

Remarks

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 permissions described in the Permissions section. If you do not have the appropriate permissions, a SecurityException occurs.

For more information about handling events, see Consuming Events.

Example

[Visual Basic, C#, C++] The following sample demonstrates the AssemblyResolve event.

[Visual Basic, C#, C++] 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;
   }
}

[C++] 
public __gc class MyType {
public:
   MyType() {
      Console::WriteLine(S"MyType instantiated!");
   }
};

__gc class Test {
public:
   static void InstantiateMyType(AppDomain* domain) {
      try {
         // You must supply a valid fully qualified assembly name here.
         domain->CreateInstance(S"Assembly text name, Version, Culture, PublicKeyToken", S"MyType");
      } catch (Exception* e) {
         Console::WriteLine(e->Message);
      }
   }
   static Assembly* MyResolveEventHandler(Object* sender, ResolveEventArgs* args) {
      Console::WriteLine(S"Resolving...");
      return __typeof(MyType)->Assembly;
   }
};

int main() {
   AppDomain*  currentDomain = AppDomain::CurrentDomain;

   Test::InstantiateMyType(currentDomain);    // Failed!

   currentDomain->AssemblyResolve += new ResolveEventHandler(0, Test::MyResolveEventHandler);

   Test::InstantiateMyType(currentDomain);    // OK!
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

.NET Framework Security: 

See Also

AppDomain Class | AppDomain Members | System Namespace

Show:
© 2014 Microsoft