There is not a lot of useful metadata in regular lambda. Instead, you can use expression trees:
void When(Expression<Func<Person, bool>> condition) { var person = new Person(); if (!condition.Compile()(person)) { Console.WriteLine("Condition not met: " + condition); return; } }
Then on the call site:
When(x => false);
And the output will be:
Condition not met: x => False
However, expression trees introduce a lot more overhead, and condition.Compile
also not cheap. Therefore, I do not recommend this approach at all, but it will display useful information as you want.
source share