I have to say that I think Richard was a little hard done by - his answer is just as good as Jason's in that they both only answered half of the question. Put them both together and you have the full answer.
To make this work with IDbCommand
, DbCommand
& SqlCommand
there has to be an explicit implementation of IDbCommand
in DbCommand
(Jason's answer) and public method shadowing in SqlCommand
(Richard's answer).
Ik geef het volledige "Foo/Bar" -voorbeeld.
Begin met deze interfaces:
public interface IFoo
{
IBar GetBar();
}
public interface IBar { }
Volgende Foo
moet een expliciete implementatie van IFoo
bevatten om Bar
, niet IBar
, vanuit zijn eigen < code> GetBar methode:
public abstract class Foo : IFoo
{
IBar IFoo.GetBar()
{
return this.GetBar();
}
public Bar GetBar()
{
return this.GetBarInner();
}
protected abstract Bar GetBarInner();
}
public abstract class Bar : IBar { }
En ten slotte moet een SomeFoo
-klasse GetBar
overschaduwen om een SomeFoo
-instantie te kunnen retourneren:
public class SomeFoo : Foo
{
public new SomeBar GetBar()
{
return new SomeBar();
}
protected override Bar GetBarInner()
{
return this.GetBar();
}
}
public class SomeBar : Bar { }
Ik denk dat de enige informatie die Richard is dat mijn toevoeging van het nieuwe
sleutelwoord aan de overschaduwde methode je de compileerfout doet verdwijnen.