从表达式调用方法

发布于 2024-07-12 06:00:48 字数 1392 浏览 5 评论 0原文

使用 Expression.Call 时,“Any”方法采用什么类型和参数?

我有一个内部表达式和一个外部表达式,我想将它们与 Any 一起使用。 表达式是以编程方式构建的。

内部(有效):

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); 

外部(看起来正确):

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

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

这给了我 2 个表达式:

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

我正在寻找的是:

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

我正在尝试使用 Expression.Call 方法来使用“Any”。 1. 这是正确的做法吗? 2.下面抛出异常, “‘System.Linq.Queryable’类型上的任何方法‘Any’都与提供的参数兼容。”

以下是我调用 Any 的方法:

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

Any 调用是如何从vehicleExpression 链接到tankFunction 的?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蘸点软妹酱 2024-07-19 06:00:48

当我尝试让 string.Contains 工作时,我遇到了类似的问题; 我只是使用了 GetMethod/MethodInfo 方法; 然而 - 它很复杂,因为它是一个通用方法...

这应该是正确的 MethodInfo - 但如果没有对 Tank 更加清晰的了解,很难给出完整的(可运行的)答案Vehicle

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

请注意,扩展方法是向后工作的 - 因此您实际上想要使用两个参数(源和谓词)调用 method

比如:

   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);

如果有疑问,请使用反射器(并稍微摆弄一下;-p) - 例如,我根据您的规范编写了一个测试方法:

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

并对它进行反编译并玩弄它......

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文