Dynamic method selection based on runtime parameter type

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.

+7
source share
3 answers

The dynamic keyword is actually really good:

 void Main() { var foobar = new FooBar(); foreach(IObject obj in new IObject[]{ new ObjectType1(), new ObjectType2()}) { foobar.Foo((dynamic)obj); } // Output: // Type 1 // Type 2 } class IObject { } class ObjectType1 : IObject { } class ObjectType2 : IObject { } class FooBar { public void Foo (ObjectType1 obj) { Console.WriteLine("Type 1"); } public void Foo (ObjectType2 obj) { Console.WriteLine("Type 2"); } } 

The code is super simple, and it got pretty decent performance .

+10
source

Perhaps you need something very close to the visitor .

If you just want to select the ( Foo ) method of an object ( FooBar ) by argument type, you can use reflection to get all the methods with the given name ( Foo ) and manually map the arguments to the object type ( ObjectTypeX ) and select the argument. Remember to handle derived classes correctly.

To avoid paying the cost of reflection for each result of the call cache (i.e., by creating / compiling an expression tree).

Note that the Dictionary approach (actions for this method, indexed by type) will probably be easier to maintain / debug for starters. Later, if you find it slow for yourself, you can replace it with something more complex, like reflection.

+2
source

you can just make one method that takes an interface type, then you check for validity and drop it

 ObjectType1 obj1 = new ObjectType1(); foo(obj1); void foo(IObject fm) { ObjectType1 cls; if (fm is ObjectType1) { cls = fm as ObjectType1; cls.ID = 10; MessageBox.Show(cls.ID.ToString() + " " + cls.GetType().ToString()); } } 

thats, because classes implement IObject, cls.ID is just an example that you can put in one property that you implement. Please try and let me know .... best wishes.

0
source

All Articles