Since COM objects hold non-memory resources but don't support IDisposable, I'm trying out some ideas to fake it. What do you think of my first attempt?
Public Function ComBlock(ByVal ParamArray comObjects As Object()) As IDisposable
For i As Integer = 0 To comObjects.Length - 1
If comObjects(i) Is Nothing Then Throw New ArgumentNullException("Null in parameter " & i)
If Not Marshal.IsComObject(comObjects(i)) Then Throw New ArgumentException(comObjects(i).GetType.Name & " is not a COM object")
Next
Return New ComContext(comObjects)
End Function
Private Class ComContext : Implements IDisposable
Private m_ComObjects As Object()
Private m_Disposed As Boolean
Sub New(ByVal comObjects As Object())
m_ComObjects = comObjects
End Sub
Private Sub Dispose() Implements IDisposable.Dispose
For Each item In m_ComObjects
Marshal.FinalReleaseComObject(item)
Next
GC.SuppressFinalize(Me)
End Sub
End Class