I have seen similar questions / answers for this published in the past, but mine are slightly different from the others that I have seen.
Essentially, I have a common interface and several classes that implement / inherit it. Then, in a separate class, I have methods that should act on the objects defined by the IObject interface. However, each of them must act differently, so there is a separate method declaration for each particular type that extends IObject.
class IObject { ... } class ObjectType1 : IObject { ... } class ObjectType2 : IObject { ... } class FooBar { void Foo (ObjectType1 obj); void Foo (ObjectType2 obj); }
Now for me, one obvious solution is to use dynamic linking by placing the Foo method inside each individual class, which will automatically select the correct Foo to execute at runtime. However, the parameter is not here, because I define several models for how to act on these objects, and I would prefer to encapsulate each individual model to process objects in my class, rather than using all models in object classes.
I found this article, which shows how to use a dictionary for dynamic selection during the execution of the correct method implementation. I am fine with this approach; however, suppose I have to do this sending once in each model. If I only have IObject and its specific implementations, is there a way to generalize this approach so that I can call methods of any name based on the type of runtime?
I know this is probably an obscure question, but I would really appreciate any help.
TSM
source share