I have a class that is generated by some tool, so I cannot change it. The generated class is very simple (no interface, no virtual methods):
class GeneratedFoo { public void Write(string p) { } }
In the C # project, we want to provide a way so that we can connect to another MyFoo implementation. So I'm going to make MyFoo from GeneratedFoo
class MyFoo : GeneratedFoo { public new void Write(string p) { } }
Then I have a CreateFoo method that will either return an instance of the GeneratedFoo class, or MyFoo. However, it always calls a method in GeneratedFoo.
GeneratedFoo foo = CreateFoo(); // if this returns MyFoo, foo.Write("1"); // it stills calls GeneratedFoo.Write
This is expceted as it is not a virtual method. But I am wondering if there is a way (possibly a hack) to force it to call a derived method.
Thanks,
Ian
inheritance design c # class
Henggyi
source share