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