When can MemberExpression.Expression be null?

155 views Asked by At

I wrote a simple utility method that accepts a lambda expression of the form x => x.PropertyName, and I want to validate that the expression is indeed of the correct form (the method then does something with the expression which is unrelated to the question). Generally I wrote the code (and unit tests) and it seems I can correctly determine whether the expression is in the correct form or not, but there's one thing I have a doubt about:

I see that MemberExpression.Expression is declared nullable (Expression?), but I couldn't think of any case where it could be null, and therefore I can't determine whether it's a valid case I should support, or not. What expression would cause MemberExpression.Expression to be null?

1

There are 1 answers

0
Arnon Axelrod On

Found the answer! If the expression is an access to a static property, like this: x => MyClass.MyStaticProperty. In this case MemberExpression.Expression will be null.