我有一个类 bMainframe,它管理与 4 个不同大型机的连接。它允许以特定方式打开相同的底层非托管库,并且一次可以连接多个大型机。每个库都有自己的非托管大型机连接资源的处置代码。包装器还具有调用各个大型机连接的处理代码的代码。
如果某人的项目没有使用所有 4 个大型机,而是调用包装器上的处置,这会导致错误。(FileLoadException 无法加载 4 个托管大型机的程序集 X)因为该处置代码检查 4 个中的哪一个不是空的/空的。即使没有任何内容/null,这也会导致 .net 尝试加载程序集并崩溃。
外包装中的处理代码是否有用或必要?有没有办法检查一个类型的程序集是否已加载,但不会触发.net 加载类型/程序集?
我修改了下面的代码以阻止 fileloadexception,但我不认为这是最好的方法。
Protected Overridable Sub Dispose(ByVal disposing As Boolean)
If Not Me.disposedValue Then
If disposing Then
' TODO: free managed resources when explicitly called
End If
Try
If Me._Linx IsNot Nothing Then
If _Linx.cnLinx IsNot Nothing Then
Try
_Linx.Disconnect()
Catch ex As Exception
Trace.WriteLine("Error doing linx.disconnectSession")
End Try
Try
_Linx.Dispose()
Catch ex As Exception
Trace.WriteLine("Error doing linx.dispose")
End Try
End If
End If
Catch ex As IO.FileLoadException
Debug.WriteLine("Failed to load LinxFile")
End Try
Try
If Me._Acaps IsNot Nothing Then
_Acaps.Disconnect()
_Acaps.Dispose()
End If
Catch ex As IO.FileLoadException
Debug.WriteLine("Failed to load AcapsFile")
End Try
Try
If Me._Dart IsNot Nothing Then
Try
_Dart.Dispose()
Catch ex As Exception
Trace.WriteLine("Error disposing of Dart")
End Try
End If
Catch ex As IO.FileLoadException
Debug.WriteLine("Failed to load DartFile")
End Try
Try
If LpsOpen Then
Try
_Lps.Dispose()
Catch ex As Exception
Trace.WriteLine("Error disposing of Lps")
End Try
End If
Catch ex As IO.FileLoadException
Debug.WriteLine("Failed to load LpsFile")
End Try
' TODO: free shared unmanaged resources
End If
Me.disposedValue = True
End Sub