I am trying to create an expression tree that represents the following:
myObject.childObjectCollection.Any(i => i.Name == 'name');
Shortened for clarity, I have the following:
//'myObject.childObjectCollection' is represented here by 'propertyExp' //'i => i.Name == 'name'' is represented here by 'predicateExp' //but I am struggling with the Any() method reference - if I make the parent method //non-generic Expression.Call() fails but, as per below, if i use <T> the //MethodInfo object is always null - I can't get a reference to it private static MethodCallExpression GetAnyExpression<T>(MemberExpression propertyExp, Expression predicateExp) { MethodInfo method = typeof(Enumerable).GetMethod('Any', new[]{ typeof(Func<IEnumerable<T>, Boolean>)}); return Expression.Call(propertyExp, method, predicateExp); }
What am I doing wrong? Anyone have any suggestions?
There are several things wrong with how you’re going about it.
You’re mixing abstraction levels. The T parameter to
GetAnyExpression<T>could be different to the type parameter used to instantiatepropertyExp.Type. The T type parameter is one step closer in the abstraction stack to compile time – unless you’re callingGetAnyExpression<T>via reflection, it will be determined at compile time – but the type embedded in the expression passed aspropertyExpis determined at runtime. Your passing of the predicate as anExpressionis also an abstraction mixup – which is the next point.The predicate you are passing to
GetAnyExpressionshould be a delegate value, not anExpressionof any kind, since you’re trying to callEnumerable.Any<T>. If you were trying to call an expression-tree version ofAny, then you ought to pass aLambdaExpressioninstead, which you would be quoting, and is one of the rare cases where you might be justified in passing a more specific type than Expression, which leads me to my next point.In general, you should pass around
Expressionvalues. When working with expression trees in general – and this applies across all kinds of compilers, not just LINQ and its friends – you should do so in a way that’s agnostic as to the immediate composition of the node tree you’re working with. You are presuming that you’re callingAnyon aMemberExpression, but you don’t actually need to know that you’re dealing with aMemberExpression, just anExpressionof type some instantiation ofIEnumerable<>. This is a common mistake for people not familiar with the basics of compiler ASTs. Frans Bouma repeatedly made the same mistake when he first started working with expression trees – thinking in special cases. Think generally. You’ll save yourself a lot of hassle in the medium and longer term.And here comes the meat of your problem (though the second and probably first issues would have bit you if you had gotten past it) – you need to find the appropriate generic overload of the Any method, and then instantiate it with the correct type. Reflection doesn’t provide you with an easy out here; you need to iterate through and find an appropriate version.
So, breaking it down: you need to find a generic method (
Any). Here’s a utility function that does that:However, it requires the type arguments and the correct argument types. Getting that from your
propertyExpExpressionisn’t entirely trivial, because theExpressionmay be of aList<T>type, or some other type, but we need to find theIEnumerable<T>instantiation and get its type argument. I’ve encapsulated that into a couple of functions:So, given any
Type, we can now pull theIEnumerable<T>instantiation out of it – and assert if there isn’t (exactly) one.With that work out of the way, solving the real problem isn’t too difficult. I’ve renamed your method to CallAny, and changed the parameter types as suggested:
Here’s a
Main()routine which uses all the above code and verifies that it works for a trivial case: