Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
258 views
in Technique[技术] by (71.8m points)

c# - Calling a Method from an Expression

What types and arguments does the method, "Any" when using Expression.Call take?

I have an inner and an outer Expression that I would like to use with Any. The expressions are built programatically.

Inner (this works):

ParameterExpression tankParameter = Expression.Parameter(typeof(Tank), "t");
Expression tankExpression = Expression.Equal(
    Expression.Property(tankParameter, "Gun"),
    Expression.Constant("Really Big"));

Expression<Func<Tank, bool>> tankFunction = 
    Expression.Lambda<Func<Tank, bool>>(tankExpression, tankParameter); 

Outer (looks correct):

ParameterExpression vehicleParameter = Expression.Parameter(typeof(Vehicle), "v");

Expression vehicleExpression = Expression.Lambda(
    Expression.Property(
        vehicleParameter, 
        typeof(Vehicle).GetProperty("Tank")), 
    vehicleParameter);

This gives me 2 expressions:

v => v.Tank
t => t.Gun == "Really Big";

And I am looking for is:

v => v.Tank.Any(t => t.Gun == "Really Big");

I am attempting to use the Expression.Call method to use, "Any". 1. Is that the right way to do it? 2. The following throws an exception, "No method 'Any' on type 'System.Linq.Queryable' is compatible with the supplied arguments."

Here is how I am calling Any:

Expression any = Expression.Call(
    typeof(Queryable),
    "Any",
    new Type[] { tankFunction.Body.Type }, // this should match the delegate...
    tankFunction);

How is the Any called chained from vehicleExpression to tankFunction?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I had a similar problem when trying to get string.Contains to work; I just used the GetMethod/MethodInfo approach instead; however - it is complicated because it is a generic method...

This should be the right MethodInfo - but it is hard to give a full (runnable) answer without a bit more clarity on Tank and Vehicle:

   MethodInfo method = typeof(Queryable).GetMethods()
        .Where(m => m.Name == "Any"
            && m.GetParameters().Length == 2)
        .Single().MakeGenericMethod(typeof(Tank));

Note that extension methods work backwards - so you actually want to call method with the two args (the source and the predicate).

Something like:

   MethodInfo method = typeof(Queryable).GetMethods()
        .Where(m => m.Name == "Any" && m.GetParameters().Length == 2)
        .Single().MakeGenericMethod(typeof(Tank));

    ParameterExpression vehicleParameter = Expression.Parameter(
        typeof(Vehicle), "v");
    var vehicleFunc = Expression.Lambda<Func<Vehicle, bool>>(
        Expression.Call(
            method,
            Expression.Property(
                vehicleParameter,
                typeof(Vehicle).GetProperty("Tank")),
            tankFunction), vehicleParameter);

If in doubt, use reflector (and fiddle it a bit ;-p) - for example, I wrote a test method as per your spec:

Expression<Func<Vehicle, bool>> func = v => v.Tank.Any(
    t => t.Gun == "Really Big");

And decompiled it and toyed with it...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...