IDisposable的實現,我有下面的類層次結構 -在抽象類層次
//Abstract Class1 in library1 (Can't modify this)
public abstract class absClass1 : IDisposable
{
public abstract int AddTwoNumbers(int Num1, int Num2);
// Some other overrides, abstract methods and concrete methods
void Dispose()
{
// Standard Dispose impl.
}
}
//Abstract Class2 in library2
public abstract class absClass2 : absClass1
{
//Implementing AddTwoNumbers
public override int AddTwoNumbers(int Num1, int Num2)
{
return Num1+Num2;
}
public abstract int MultiplyTwoNumbers(int Num1, int Num2);
// Some other overrides, abstract methods and concrete methods
protected override void Dispose(bool disposing)
{
if (!IsDisposed)
{
//Cleanup
}
base.Dispose(disposing);
}
}
//Derived class from absClass2 in library3
public sealed class absDerived1 : absClass2
{
//Implementing MultiplyTwoNumbers
public override int MultiplyTwoNumbers(int Num1, int Num2)
{
return Num1*Num2;
}
// Some other overrides, abstract methods and concrete methods
protected override void Dispose(bool disposing)
{
if (!IsDisposed)
{
// Cleanup
}
base.Dispose(disposing);
}
}
//... Some more implementation of absClass2 like absDerived2 etc.
// can be there in other library4...
我想在處置兩absClass2
和absDerived
對象,有沒有在這兩個重寫Dispose(布爾)這些類的任何問題?這個設計有什麼問題嗎?這怎麼能改進?
你在哪裏執行GC.SuppressFinalize(this); ? –
@MAfifi,我不會使用GC.SuppressFinalize,因爲我們沒有使用任何析構函數,也不想自動執行Dispose調用,所以我們依靠確保所有對象都被顯式地Dispose。 – akjoshi