从表达式调用方法
使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当我尝试让
string.Contains
工作时,我遇到了类似的问题; 我只是使用了GetMethod
/MethodInfo
方法; 然而 - 它很复杂,因为它是一个通用方法...这应该是正确的
MethodInfo
- 但如果没有对Tank 更加清晰的了解,很难给出完整的(可运行的)答案
和Vehicle
:请注意,扩展方法是向后工作的 - 因此您实际上想要使用两个参数(源和谓词)调用
method
。比如:
如果有疑问,请使用反射器(并稍微摆弄一下;-p) - 例如,我根据您的规范编写了一个测试方法:
并对它进行反编译并玩弄它......
I had a similar problem when trying to get
string.Contains
to work; I just used theGetMethod
/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 onTank
andVehicle
:Note that extension methods work backwards - so you actually want to call
method
with the two args (the source and the predicate).Something like:
If in doubt, use reflector (and fiddle it a bit ;-p) - for example, I wrote a test method as per your spec:
And decompiled it and toyed with it...