How can I call a base interface method?
class ThirdPartyClass :IDisposable { //I can not modify this class
void IDisposable.Dispose() {
Console.WriteLine( "ThirdPartyClass Dispose" );
}
}
class MyClass :ThirdPartyClass, IDisposable {
void IDisposable.Dispose() {
Console.WriteLine( "MyClass Dispose" );
//base.IDisposable.Dispose(); //How can I do this !!!!
}
}
Hi,
Usually when a base class implements an IDisposable, it also implements
a "protected virtual void Dispose(bool disposing)" where the acctually
cleanup is done... (if it is done correctly).
and the Dispose() method calls it.
try to override it as follow:
class ThirdPartyClass :IDisposable
{ //I can not modify this class
void IDisposable.Dispose()
{
GC.SuppressFinalize(this);
Dispose(false);
}
protected virtual void Dispose(bool disposing)
{
Console.WriteLine( "ThirdPartyClass Dispose" );
}
}
class MyClass :ThirdPartyClass, IDisposable
{
protected override void Dispose(bool disposing)
{
// Do your cleanup here
Console.WriteLine( "MyClass Dispose" );
base.Dispose(disposing);
}
}
if it doesn't implement a Dispose(bool disposing)
then you should do as you did, only:
class ThirdPartyClass :IDisposable
{ //I can not modify this class
void IDisposable.Dispose()
{
Console.WriteLine( "ThirdPartyClass Dispose" );
}
}
class MyClass :ThirdPartyClass, IDisposable
{
void IDisposable.Dispose()
{
Console.WriteLine( "MyClass Dispose" );
base.Dispose(); // just call the base class implementation of the
Dispose.
}
}
Cheers,
Eyal.