public
class
MyResourceHog : IDisposable
{
private
bool
_alreadyDisposed =
false
;
~MyResourceHog()
{
Dispose(
false
);
}
public
void
Dispose()
{
Dispose(
true
);
GC.SuppressFinalize(
true
);
}
protected
virtual
void
Dispose(
bool
isDisposing )
{
if
( _alreadyDisposed )
return
;
if
( isDisposing )
{
}
_alreadyDisposed =
true
;
}
}
如果衍生类需要执行另外的清除操作,它应该实现受保护的Dispose方法:
public
class
DerivedResourceHog : MyResourceHog
{
private
bool
_disposed =
false
;
protected
override
void
Dispose(
bool
isDisposing )
{
if
( _disposed )
return
;
if
( isDisposing )
{
}
base
.Dispose( isDisposing );
_disposed =
true
;
}
}