如果我的类仅使用托管资源,所以我认为没有必要完全实现IDisposable模式。
这应该是足够的:
public class ManagedResourceClient : IDisposable
{
private ITheManagedResource _myManagedResource = new TheManagedResource()
public void Dispose()
{
if ( _myManagedResource != null )
{
_myManagedResource.Dispose();
_myManagedResource = null;
}
}
}
我看不到使用下列方法的理由:
- 使用终结器,因为它只使用了实现了IDisposable接口的托管资源
- 使用“disposing”标志,因为可以通过检查是否为空来处理
- 使用虚拟的Disposing方法,因为没有必要区分GC调用和直接调用。
IDisposable
接口的对象。 - Tim Schmelter