使用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);
Run Code Online (Sandbox Code Playgroud)
外面(看起来正确):
ParameterExpression vehicleParameter = Expression.Parameter(typeof(Vehicle), "v");
Expression vehicleExpression = Expression.Lambda(
Expression.Property(
vehicleParameter,
typeof(Vehicle).GetProperty("Tank")),
vehicleParameter);
Run Code Online (Sandbox Code Playgroud)
这给了我两个表达式:
v => v.Tank
t => t.Gun == "Really Big";
Run Code Online (Sandbox Code Playgroud)
而我正在寻找的是:
v => v.Tank.Any(t => t.Gun == "Really Big");
Run Code Online (Sandbox Code Playgroud)
我试图使用Expression.Call方法来使用"Any".这是正确的方法吗?2.以下引发异常,"类型'System.Linq.Queryable'上没有方法'Any'与提供的参数兼容."
这是我如何调用任何:
Expression any = Expression.Call(
typeof(Queryable),
"Any",
new Type[] { tankFunction.Body.Type }, // this should match the delegate...
tankFunction);
Run Code Online (Sandbox Code Playgroud)
如何将来自vehicleExpression的Any链接到tankFunction?
我在尝试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));
Run Code Online (Sandbox Code Playgroud)
请注意,扩展方法可以向后工作 - 因此您实际上希望method使用两个args(源和谓词)进行调用.
就像是:
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);
Run Code Online (Sandbox Code Playgroud)
如果有疑问,请使用反射器(并稍微调整一下;-p) - 例如,我根据您的规范编写了一个测试方法:
Expression<Func<Vehicle, bool>> func = v => v.Tank.Any(
t => t.Gun == "Really Big");
Run Code Online (Sandbox Code Playgroud)
反编译它并玩弄它......