Updated: December 2009
Occurs when the resolution of an assembly fails.
Namespace:
System
Assembly:
mscorlib (in mscorlib.dll)
Visual Basic (Declaration)
Public Event AssemblyResolve As ResolveEventHandler
Dim instance As AppDomain
Dim handler As ResolveEventHandler
AddHandler instance.AssemblyResolve, handler
public event ResolveEventHandler AssemblyResolve
public:
virtual event ResolveEventHandler^ AssemblyResolve {
void add (ResolveEventHandler^ value);
void remove (ResolveEventHandler^ value);
}
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. 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 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.
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
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;
}
}
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 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
Reference
Date | History | Reason |
|---|
December 2009
| Clarification: The returned assembly must be loaded into an execution context, not the reflection-only context. |
Customer feedback.
|