ToString on expression trees produces poorly formatted output

When I use Expression.ToString()to convert an expression tree into a human-readable form, the result looks something like this:

x => ((x.ID > 2) OrElse (x.ID != 6))
x => ((x.ID > 2) AndAlso (x.ID != 6))

Ideally, I would like the output to display operators instead of "OrElse" and "AndAlso":

x => ((x.ID > 2) || (x.ID != 6))
x => ((x.ID > 2) && (x.ID != 6))

As a workaround, I could use a method string.Replace()..

.Replace("AndAlso", "&&")
.Replace("OrElse", "||")

but it has obvious flaws and seems uncomfortable. Also, I don’t want to create a large Replace section or a huge tree of regular expressions just for proper formatting.

Is there an easy way to get a code-readable form of expression trees?

+4
source share
1

, - ExpressionVisitor, #.

- ExpressionStringBuilder , .

0

Source: https://habr.com/ru/post/1539073/


All Articles