Some trouble with UnaryExpressions.
This works this way:
Expression<Func<List<string>, object>> k = l => l.Count;
//got member in this case like this
var member = ((k.Body as UnaryExpression).Operand as MemberExpression).Member;
In the above case the k.Body.NodeType was ExpressionType.Convert. But it's a little tricky with ExpressionType.ArrayLength. How would I get the PropertyInfo member similarly in the below case?:
Expression<Func<string[], int>> k = l => l.Length;
var member = ??
In the second case k.Body is something like ArrayLength(l).
I can do it with a hack like this:
var member = (k.Body as UnaryExpression).Operand.Type.GetProperty("Length");
but this doesn't feel like a straight forward expression approach. It's more a plain old reflection call with dirty string "Length" passed. Is there a better way?
It's an
ArrayLengthnode, which you can create with theExpression.ArrayLengthmethod.It's just a
UnaryExpressionwith anOperandwhich is the array expression, and aNodeTypeofArrayLength. It's not entirely clear to me what you wanted to know about it, but hopefully the call toExpression.ArrayLengthis what you were after.EDIT: Although there is an
Array.Lengthproperty, that's not what's used normally. For example:... then evaluating
x.Lengthuses theldlenIL instruction whereas evaluatingy.Lengthuses a call to the property.