开发者

How to get the referred string of an Action<string> delegate?

I have a method that expects an Action<string>

I call the method as follows:

commandProcessor.ProcessCommand(s=> ShowReceipt("MyStringValue"))

开发者_JAVA技巧
ProccessCommand(Action<string> action)
{
  action.Invoke(...); // How do I get the reffered string?
}

Do I have to use Expression<Action<string>> ? If so, how do I get the parameter values?


You would indeed have to use Expression<Action<string>>... and even then you'd have to make some assumptions or write quite a lot of code to make it very robust.

This post may help you - it's pretty similar - but I would try to think of an alternative design if possible. Expression trees are great, and very interesting... but I typically think of them as a bit of a last resort.


Usually you would call it like this:

 commandProcessor.ProcessCommand(s=> ShowReceipt(s)) 

or simply

 commandProcessor.ProcessCommand(ShowReceipt)

and supply the string to the action in the called method:

 ProcessCommand(Action<string> action) 
 { 
  action("MyStringValue"); 
 } 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜