我有一个函数具有以下签名...
I have a function that has the following signature...
public string DoJunk(Expression<Func<bool>> expression)
我试图找到一种方式来恩pression参数转换回类似的东西原始出处code(或原烃源$ c。至少AC#重presentation $ C)。因此,如果有人调用函数像这样...
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))
...我希望能够转换前pression这...
...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?
下面是一个有趣的文章,用code,讨论EX pression树转换回一些类似于(大致)的原始来源:
Here's an interesting article, with code, discussing the conversion of expression trees back into something that resembles (roughly) the original source:
防爆pression树-Lambda表达式为codeDOM转换
作为一个侧面说明,你有没有打过电话的前$ P $ pssion的的ToString
的方法?
As a side-note, have you tried calling the expression's ToString
method?
Expression<Func<int, int, bool>> expr =
(i, j) => (i + j) * 9 == Math.Round((double)j / (i - 3), 4);
Console.WriteLine(expr.ToString());
// (i, j) => (Convert(((i + j) * 9)) = Round((Convert(j) / Convert((i - 3))), 4))
Console.WriteLine(expr.Body.ToString());
// (Convert(((i + j) * 9)) = Round((Convert(j) / Convert((i - 3))), 4))