I need to print a linq expression as it is written in the source code. I have searched for solutions, but so far the recommendation is to use expr.ToString().
This won't work for me. For example, if I have an expression:
Expression<Func<int, int>> expr = v => v + 1;
ToString() would return v => (v + 1)
Now if I make a minor modification to the above case
var a = 3;
Expression<Func<int, int>> expr = v => v + a;
then ToString() returns v => (v + value(Program+<>c__DisplayClass0_0).a). I am looking for something like v => (v + a)
My current use case is that I have a set of rules declared as Expression<Func<TSource, ValidationResult>>, then I need to benchmark how long it took to execute them, and for that I need to label them properly. ToString() on a rule will produce a string that is not very readable; just like the above example anything not defined in the calling class will be prefixed with a long string indicating where the token is from.
v => v + myMethod()? There´s no single solution to this, you have to do this yourself.Regexremoval ofvalue(.+)\.wouldn't be good enough?