将表达式树转换为源代码字符串
我有一个具有以下签名的函数...
public string DoJunk(Expression<Func<bool>> expression)
我试图找到一种方法将“表达式”参数转换回类似于原始源代码的内容(或至少是原始源代码的 ac# 表示形式)。所以,如果有人像这样调用函数...
DoJunk(() => (i + j) * 9 == Math.Round((double)j / (i - 3), 4))
...我希望能够将表达式转换为这样...
(i + j) * 9 == Math.Round((double)j / (i - 3), 4)
有人这样做过吗?
I have a function that has the following signature...
public string DoJunk(Expression<Func<bool>> expression)
I'm trying to find a way to convert the "expression" parameter back to something resembling the original source code (or at least a c# representation of the original souce code). So, if someone calls the function like this...
DoJunk(() => (i + j) * 9 == Math.Round((double)j / (i - 3), 4))
...I'd like to be able to convert the expression to this...
(i + j) * 9 == Math.Round((double)j / (i - 3), 4)
Has anyone done this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我刚刚遇到过这个;我编写了一个免费的开源库,它提供了一种扩展方法来从表达式创建类似源代码的字符串:
我编写了 关于它的博客,来源是 在 GitHub 上,有包含扩展方法的 NuGet 包,以及我为 VS 2010 编写了一组调试器可视化工具 -> 2022 年,位于 Visual Studio Marketplace 中。
I've just happened across this; I've written a free, open-source library which provides an extension method to create a source-code-like string from an Expression:
I've written a blog about it, the source is on GitHub, there's a NuGet package containing the extension method, and I've written a set of Debugger Visualizers for VS 2010 -> 2022 which are in the Visual Studio Marketplace.
这是一篇有趣的文章,其中包含代码,讨论将表达式树转换回类似于(大致)原始源的内容:
作为旁注,您是否尝试过调用表达式的
ToString方法?
Here's an interesting article, with code, discussing the conversion of expression trees back into something that resembles (roughly) the original source:
As a side-note, have you tried calling the expression's
ToString
method?