This is an aspnetcore class library project I have an interface
public interface ITest
{
string TestMethod(string name);
}
and a class that implements the interface
public class Test : ITest
{
public virtual string TestMethod(string name)
{
return "hello " + name;
}
}
to prove that this works, I have a console application where I make a simple call
using TestProject.Test;
public class Tester
{
public void TestMethod()
{
var testClass = new Test();
Console.WriteLine(testClass.TestMethod("John")); // Print "hello John"
}
}
Now, I created a new project, also from aspnetcore class library, I imported the first project, and I want to overwrite the method
using TestProject.Test;
public class TestOverwritten : Test
{
public override string TestMethod(string name)
{
return "(overwritten) hello " + name;
}
}
My question is: how can I make the call to this overwritten method, without having to modify the call from the console application? it's possible? If so, is there a concept that defines what I want to do? (will it be IoC, or maybe encapsulation?)
using TestProject.Test;
public class Tester
{
public void TestMethod()
{
var testClass = new Test(); // I assume this line should be different ... but how?
Console.WriteLine(testClass.TestMethod("John")); // This should print "(overwritten) hello John"
}
}
when I say "without having to modify the call from the console application" I mean that, the creation of the object "var testClass = new Test ();" be generic enough to not need to be modified, but still recognize that you should no longer seek the implementation of the method to the class "Test", but to the class "TestOverwritten". Perhaps through a configuration file that reads the classes that implement the interface? that way you could modify the configuration file (from "Test" to "TestOverwritten"), and not have to modify all the methods that invoke the "Test" class.