I have a class that is generated by some tool, therefore I can't change it. The generated class is very simple (no interface, no virtual methods):
class GeneratedFoo
{
public void Write(string p) { /* do something */ }
}
In the C# project, we want to provide a way so that we can plug in a different implementation of MyFoo. So I'm thinking to make MyFoo derived from GeneratedFoo
class MyFoo : GeneratedFoo
{
public new void Write(string p) { /* do different things */ }
}
Then I have a CreateFoo method that will either return an instance of GeneratedFoo or MyFoo class. However it always calls the method in GeneratedFoo.
GeneratedFoo foo = CreateFoo(); // if this returns MyFoo,
foo.Write("1"); // it stills calls GeneratedFoo.Write
This is expceted since it is not a virtual method. But I'm wondering if there is a way (a hack maybe) to make it call the derived method.
Thanks,
Ian