AppDomain.AssemblyResolve Event occurs when the resolution of an assembly fails. : Application Domain « Reflection « VB.Net






AppDomain.AssemblyResolve Event occurs when the resolution of an assembly fails.

 

Imports System.Reflection

Public Class MyType
    Public Sub New()
        Console.WriteLine("MyType instantiated!")
    End Sub
End Class
Class Test
    Public Shared Sub Main()
        Dim currentDomain As AppDomain = AppDomain.CurrentDomain
        InstantiateMyTypeFail(currentDomain)
        AddHandler currentDomain.AssemblyResolve, AddressOf MyResolveEventHandler
        InstantiateMyTypeFail(currentDomain)
        InstantiateMyTypeSucceed(currentDomain)
    End Sub
    Private Shared Sub InstantiateMyTypeFail(ByVal domain As AppDomain)
        Try
            domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyType")
        Catch e As Exception
            Console.WriteLine()
            Console.WriteLine(e.Message)
        End Try
    End Sub
    Private Shared Sub InstantiateMyTypeSucceed(ByVal domain As AppDomain)
        Try
            Dim asmname As String = Assembly.GetCallingAssembly().FullName
            domain.CreateInstance(asmname, "MyType")
        Catch e As Exception
            Console.WriteLine()
            Console.WriteLine(e.Message)
        End Try
    End Sub
    Private Shared Function MyResolveEventHandler(ByVal sender As Object, ByVal args As ResolveEventArgs) As Assembly
        Return GetType(MyType).Assembly
    End Function
End Class

   
  








Related examples in the same category

1. System AppDomain Get Current Thread Id
2.AppDomain.GetAssemblies
3.AppDomain.AssemblyLoad Event occurs when an assembly is loaded.
4.AppDomain.CreateInstanceAndUnwrap
5.AppDomain.CreateInstanceFrom
6.AppDomain.CurrentDomain gets the current application domain for the current Thread.
7.AppDomain.ExecuteAssembly executes the assembly contained in the specified file.
8.AppDomain.GetAssemblies
9.AppDomain.GetData gets the value stored in the current application domain for the specified name.
10.AppDomain.Id Property gets an integer that uniquely identifies the application domain within the process.
11.Create an Application Domain
12.Load Assemblies into an Application Domain
13.Unload an Application Domain