Just curious if there is a solution for this without making the base class method virtual and the derived method marked override:
I want it to jump into the derived method, set the property to true (for testing purposes, almost like this is a mock), but it's jumping into the base class. The only solution is to make the base virtual, I thought I could mark it new in the derived and that would work.
public class SomeClass { private readonly IFoo _foo; public SomeClass(IFoo foo) { _foo = foo; } public void DoSomething() { _foo.DoFoo(); } } public interface IFoo { void DoFoo(); } public class Foo : IFoo { public void DoFoo() { } } public class Foo2 : Foo { public bool DoFooWasCalled { get; set; } public new void DoFoo() { DoFooWasCalled = true; base.DoFoo(); } } Here's the test that is failing:
[TestFixture] public class TestingCSInheritance { [Test] public void TestMethod() { var foo = new Foo2(); var someClass = new SomeClass(foo); someClass.DoSomething(); foo.DoFooWasCalled.ShouldBe(true); } } My guess is that because I'm using dependency injection with an interface it's using the base class, but I'm not sure why.
virtual? that is kinda the easiest and most obvious way to do this.