表达式转字符串
获得一个字符串
Namespace.IMyService.Do("1")
我怎样才能从这个片段中演示的表达式中
IMyService myService = ...;
int param1 = 1;
myExpressionService.Get(c => myService.Do(param1));
:我实际上不想调用 Do
,除非使用生成的字符串满足条件。
How can I get a string like
Namespace.IMyService.Do("1")
from the expression as demoed in this snip it:
IMyService myService = ...;
int param1 = 1;
myExpressionService.Get(c => myService.Do(param1));
I actually don't want to call Do
unless a condition is met using the string that was generated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您必须遍历
表达式树
。以下是一些示例代码:如果以这种方式调用,它会起作用:
string result = myExpressionService.Get(() => myService.Do(1));
输出为:
Namespace. IMyService.Do(1)
您可以扩展/更新它来处理您的场景。
You will have to traverse
Expression trees
. Here is some sample code:It works if called in this way:
string result = myExpressionService.Get(() => myService.Do(1));
The output is:
Namespace.IMyService.Do(1)
You can extend/update it to handle your scenario.
这也可以工作(尽管它不是特别优雅):
你可以这样调用它:
This would also work (though it's not particularly elegant):
You can call it like this:
您应该能够对结果表达式调用 .ToString() 。根据 MS 文档,ToString() 返回表达式的文本表示形式。你尝试过吗?
You should be able to call .ToString() on the resulting expression. According to the MS documentation ToString() returns the textual representation of the expression. Have you tried that?