C #: Is there a general way to redirect a method call to another object (with the same interface)?

Is there a way to implement this template in a general way?

The dispatcher object and the set of work objects come from the same interface.

Any method call to the dispatcher object must be sent (redirected) to one of the working objects (with all arguments).

Each method will need to open its own name, find the corresponding method in the work objects, find the arguments, and then make a call. If possible, do not use the variable argument mechanism.

Is there any way to do this? Reflection? Code generation?

+7
reflection c #
source share
3 answers

It may not be so simple, but very durable - look at Castle.DynamicProxy: http://kozmic.pl/dynamic-proxy-tutorial/

+5
source share

One possible approach would be for each method in the dispatcher object to raise an event, and all work objects to subscribe to this event. (Think of it as a multicast delegate template).

I suggest that this may not be exactly the β€œgeneric” one you are looking for, but it may be an easier way to achieve basically the same goals.

+1
source share

If you want to take the code-gen-route, this t4 file does something similar: http://gist.github.com/647885

0
source share

All Articles