Steve Newton
Steve Newton

Reputation: 1078

c# reflection to call dynamic method

I have seen various examples of reflection to dynamically call methods by sending a string. Most of the examples show how to very easily dynamically set the class, method and parameters. My requirement is slightly different and can't get it to work. The following line is what I would include hardcoded.

Mailer.CallBack(parameters).Send();

Mailer is the class, Callback is the method, parameters is parameters, but how on earth do I ensure it calls it dynamically with the .send() appended?

The answer escapes me so hope someone can help.

Steve.

Upvotes: 1

Views: 280

Answers (1)

Jacob Mattison
Jacob Mattison

Reputation: 51052

All it means when you chain methods like that is that you're calling the second method on the result of the first method. In other words

Mailer.CallBack(parameters).Send();

should be exactly equivalent to

var callbackResult = Mailer.CallBack(parameters);
callbackResult.Send();

so you should be able to do the equivalent of the first line using reflection, and then just make the second call normally.

(To put it another way, when you make the call to CallBack, it has no awareness of the .Send() that is appended to it. CallBack does its thing, and then returns, and then .Send() is called on the result.)

Upvotes: 2

Related Questions