Wie erstelle ich eine Ausdrucksbaumstruktur, die IEnumerable & lt; TSource & gt; .Any (...) aufruft?

Ich versuche, erstellen Sie einen Ausdruck-Baum, stellt die folgende:

myObject.childObjectCollection.Any(i => i.Name == "name");

Verkürzt aus Gründen der übersichtlichkeit habe ich den folgenden:

//'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);
}

Was mache ich falsch? Jemand irgendwelche Vorschläge?

InformationsquelleAutor der Frage flesh | 2008-11-28

Schreibe einen Kommentar