4 ms·
Is this shared somewhere?
by superjan 4y ago
Is this shared somewhere?
- torginus 4y agoNah, it was some corporate project. But if you are interested, I could rewrite it. It would be a fun weekend project.
- mattmanser 4y agoIt's also worth figuring out for yourself though! They're suprisingly easy to use (look up Expression<>). It's incredibly useful once you know how, and encourages you to stop using reflection, passing magic strings as arguments, or having to use nameof().
- mananaysiempre 4y agoFWIW, in Python, this is pytest’s entire thing (although it ends up grovelling into bytecode to achieve it).
- throwaway6977 4y agoNot quite the same, but available on nuget- 'fluentAssertions' gives you something akin to this. I've had decent success with having our juniors use it vs less verbose assertion libraries. I don't know about evaluating individual expressions in a line separately, but it does give you clean syntax and similar error messages that are very readable- "GetProductPage().GetProductPrice().Should().Be().LessThan(...)"
- opminion 4y agohttps://stackoverflow.com/a/700325 https://stackoverflow.com/a/700325 to write out the internal structure of a LambdaExpression. If you just want the assignments then it's simpler: Add to the Evaluate method a test for MemberExpression and then: The variable name is: ((MemberExpression)expr).Member.Name The value is: Expression.Lambda(expr).Compile().DynamicInvoke()