Det kan ikke lade sig gøre ved normal brug da dependent DLL bliver loadet af .NET
runtime inden din kode starter og du derfor ikke har mulighed for at kontrollere
fejl.
Hvis du bruger reflection til at loade med kan du godt catche fejlen.
Eksempel følger.
Util.vb
-------
Imports System
Public Class Util
Public Shared Function Test
Console.WriteLine("Det virker")
End Function
End Class
Test.vb
-------
Imports System
Class TestClass
Public Shared Sub Main(ByVal args As String())
Try
Util.Test()
Catch
Console.WriteLine("Vi snuppede fejlen")
End Try
End Sub
End Class
Test2.vb
--------
Imports System
Imports System.Reflection
Class TestClass
Public Shared Sub Main(ByVal args As String())
Try
Dim o As Object = [Assembly].Load("Util").CreateInstance("Util")
o.GetType().InvokeMember("Test", _
BindingFlags.DeclaredOnly Or BindingFlags.Public Or BindingFlags.NonPublic Or BindingFlags.Static Or BindingFlags.InvokeMethod, _
Nothing, Nothing, Nothing)
Catch
Console.WriteLine("Vi snuppede fejlen")
End Try
End Sub
End Class
Forsøg 1
--------
C:\E>vbc /t:library Util.vb
Microsoft (R) Visual Basic .NET Compiler version 7.10.3052.4
for Microsoft (R) .NET Framework version 1.1.4322.573
Copyright (C) Microsoft Corporation 1987-2002. All rights reserved.
C:\E>vbc /t:exe /r:Util.dll Test.vb
Microsoft (R) Visual Basic .NET Compiler version 7.10.3052.4
for Microsoft (R) .NET Framework version 1.1.4322.573
Copyright (C) Microsoft Corporation 1987-2002. All rights reserved.
C:\E>test
Det virker
C:\E>del *.dll
C:\E>test
Unhandled Exception: System.IO.FileNotFoundException: File or assembly name Util
, or one of its dependencies, was not found.
File name: "Util"
at TestClass.Main(String[] args)
=== Pre-bind state information ===
LOG: DisplayName = Util, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
(Fully-specified)
LOG: Appbase = C:\E\
LOG: Initial PrivatePath = NULL
Calling assembly : Test, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null.
===
LOG: Application configuration file does not exist.
LOG: Policy not being applied to reference at this time (private, custom, partia
l, or location-based assembly bind).
LOG: Post-policy reference: Util, Version=0.0.0.0, Culture=neutral, PublicKeyTok
en=null
LOG: Attempting download of new URL
file:///C:/E/Util.DLL.LOG: Attempting download of new URL
file:///C:/E/Util/Util.DLL.LOG: Attempting download of new URL
file:///C:/E/Util.EXE.LOG: Attempting download of new URL
file:///C:/E/Util/Util.EXE.Forsøg 2
--------
C:\E>vbc /t:library Util.vb
Microsoft (R) Visual Basic .NET Compiler version 7.10.3052.4
for Microsoft (R) .NET Framework version 1.1.4322.573
Copyright (C) Microsoft Corporation 1987-2002. All rights reserved.
C:\E>vbc /t:exe Test2.vb
Microsoft (R) Visual Basic .NET Compiler version 7.10.3052.4
for Microsoft (R) .NET Framework version 1.1.4322.573
Copyright (C) Microsoft Corporation 1987-2002. All rights reserved.
C:\E>test2
Det virker
C:\E>del *.dll
C:\E>test2
Vi snuppede fejlen