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