是否有方法或其他轻量级方法来检查引用是否属于被处置对象?

P.S。 - 这只是一种好奇心(睡得好,不在生产代码中)。是的,我知道在尝试访问对象的成员时我可以捕获 ObjectDisposedException

有帮助吗?

解决方案

这取决于, IDisposable 对象允许根据需要调用 Dispose 方法,并且有 IDisposable 对象throw ObjectDisposedException 。在这种情况下,这些对象必须跟踪状态(通常使用私有布尔字段 isDisposed 实现)。

其他提示

否 - IDisposable模式的默认实现不支持

内置任何东西都不会允许这样做。您需要公开反映内部置位标志的IsDisposed布尔属性。

public class SimpleCleanup : IDisposable
{
    private bool disposed = false;

    public bool IsDisposed
    {
       get
       {
          return disposed;
       }
    }

    public SimpleCleanup()
    {
        this.handle = /*...*/;
    }

    protected virtual void Dispose(bool disposing)
    {
        if (!disposed)
        {
            if (disposing)
            {
               // free only managed resources here
            }

            // free unmanaged resources here
            disposed = true;
        }
    }

    public void Dispose()
    {
        Dispose(true);
    }
}

Dispose 方法需要在放弃对象之前执行所需的清理工作;如果不需要清理,则不需要做任何事情。要求一个对象跟踪它是否已经被处置,即使 Dispose 方法不执行任何操作,也需要许多 IDisposable 对象来添加标志以获得非常有限的好处

如果 IDisposable 包含两个属性 - 一个指示对象是否需要处理,其中一个指示该对象尚未< em>通过处置使其无用对于处理实际执行某些操作的对象,这两个值最初都为true,并且在 Dispose 之后将变为false。对于处理不需要进行任何清理的对象,第一种方法总是返回false而第二种方法总是为真,而不必在任何地方存储标志。我认为现在没有任何方法可以添加到.NET中。

我看到这是旧的,但我没有看到答案。 有些并非像DataSet这样的所有一次性对象都有可以附加的处理事件。

class DisposeSample : IDisposable
{
    DataSet myDataSet = new DataSet();
    private bool _isDisposed;

    public DisposeSample()
    {
        // attach dispose event for myDataSet
        myDataSet.Disposed += MyDataSet_Disposed;
    }

    private void MyDataSet_Disposed(object sender, EventArgs e)
    {
        //Event triggers when myDataSet is disposed
        _isDisposed = true; // set private bool variable as true 
    }


    public void Dispose()
    {
        if (!_isDisposed) // only dispose if has not been disposed;
            myDataSet?.Dispose(); // only dispose if myDataSet is not null;
    }
}

我喜欢做的是声明对象而不初始化它们,但是将它们的默认值设置为 Nothing 。然后,在循环结束时,我写道:

If anObject IsNot Nothing Then anObject.Dispose()

以下是完整的示例:

Public Sub Example()
    Dim inputPdf As PdfReader = Nothing, inputDoc As Document = Nothing, outputWriter As PdfWriter = Nothing

    'code goes here that may or may not end up using all three objects, 
    ' such as when I see that there aren't enough pages in the pdf once I open  
    ' the pdfreader and then abort by jumping to my cleanup routine using a goto ..

GoodExit:
    If inputPdf IsNot Nothing Then inputPdf.Dispose()
    If inputDoc IsNot Nothing Then inputDoc.Dispose()
    If outputWriter IsNot Nothing Then outputWriter.Dispose()
End Sub

这也非常适合将主要对象放在例程的顶部,在 Try 例程中使用它们,然后将它们放在 Finally 块中:

Private Sub Test()
    Dim aForm As System.Windows.Forms.Form = Nothing
    Try
        Dim sName As String = aForm.Name  'null ref should occur
    Catch ex As Exception
        'got null exception, no doubt
    Finally
        'proper disposal occurs, error or no error, initialized or not..
        If aForm IsNot Nothing Then aForm.Dispose()
    End Try
End Sub
scroll top