Jon,
The problem here has to do with multiple inheritence. As you know, C# only
supports single inheritence. Of course, you can implement multiple
interfaces. But, what does this really mean?
If you think about it, it means you can only inherit one implementation.
From this persepective, the IDisposable interface cannot have
implementation. By definition, it is an interface, not an implementation.
Hence, in your derived class, you can have the Dispose() method. But, you
don't get to inherit any base class functionality.
Now to the Crux of it... But ThirdPartyClass is a class, so why can't I
inherit its functionality. And the reason is, because the Dispose method in
this class is explicitly defined as an interface only. If the base class had
been defined as:
class ThirdPartyClass : IDisposable
{ //I can not modify this class
public void Dispose()
{
Console.WriteLine("ThirdPartyClass Dispose");
}
}
or better yet:
class ThirdPartyClass : IDisposable
{ //I can not modify this class
public virtual void Dispose()
{
Console.WriteLine("ThirdPartyClass Dispose");
}
}
then you would be able to inherit from this member. In the former case you
could use the "new" keyword, and in the later case "override" to implement
your method. For example, in the former case:
public override void Dispose()
{
Console.WriteLine("MyClass Dispose");
base.Dispose(); // now this works
}
or in the later case:
public new void Dispose()
{
Console.WriteLine("MyClass Dispose");
base.Dispose(); // now this works
}
You can use some trickery to get at the base type and get its hidden
implementation. But the purpose of the explicit interface definition was to
hide it and not allow this.
The trickery involves using reflection to get to the actual implementation
of the base class, and force the hidden method invocation. For example, as
suggested by Eyal Safrin:
public virtual void Dispose()
{
Console.WriteLine("MyClass Dispose");
InterfaceMapping map =
base.GetType().BaseType.GetInterfaceMap(typeof(IDi sposable));
map.TargetMethods[0].Invoke(this, new object[] {});
}
To fix this third party library once and for all, you might then create the
following wrapper class:
class ThirdPartyWrapper : ThirdPartyClass
{
public virtual void Dispose()
{
Console.WriteLine("Wrapper class");
InterfaceMapping map =
base.GetType().BaseType.GetInterfaceMap(typeof(IDi sposable));
map.TargetMethods[0].Invoke(this, new object[] {});
}
}
class MyClass : ThirdPartyWrapper, IDisposable
{
public override void Dispose()
{
Console.WriteLine("MyClass Dispose");
base.Dispose();
}
}
Then use this class for all internal usage.
Hope this helps...
--
Frisky
Intellectuals solve problems; geniuses prevent them. ~ Albert Einstein
<jo*@martinsound.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
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 !!!!
}
}