Is there a simple way of finding out if an Expression contains a ParameterExpression that isn’t further wrapped in for example a MemberExpression.
Example:
x => x.Method() ? x : null <= 1 occurence of x without any further evaluation
x => x.Method() ? x.Property : null <= 0 occurences of x without any further evaluation
Simply put my use case is that I know the Method (no params) and Property values and want to find out if this is enough to evaluate the expression without fetching the entire “object” from store.
Edit:
My example is maybe to simplified. There are more Expression types that needs to be handled (for example UnaryExpression).
x => ((Cast) x).Property <= 0 occurences of x without any further evaluation
I’m looking for an answer to the following question:
given an expression, if I know all method return values and property values of the input parameter but not the parameter value itself, can I evaluate the expression?
If you are on .NET 4 or later, you can use
ExpressionVisitorfor this.I’m not quite sure how you define “lonely parameter”, but if you want to exclude direct method calls, member accesses and indexer accesses on parameters, you can use something like this (untested):
Use it like this: