Expression.Lambda() 的参数问题
更新:这确实有效,我很愚蠢:(
我在运行时有以下扩展方法,
public static string ExtMethod(this object self, object myparameter);
可以通过多种方式调用它,我认为这些都是可能性:
Expression<Func<T, string>> expr = x => x.property.ExtMethod(5);
Expression<Func<T, string>> expr = x => x.property.ExtMethod(new object());
Expression<Func<T, string>> expr = x => x.property.ExtMethod(someMethod());
Expression<Func<T, string>> expr = x => x.property.ExtMethod(x.someMethod());
Expression<Func<T, string>> expr = x => x.property.ExtMethod(x.OtherProperty);
我需要做的是 给定“expr
”和“T
”,评估“myparameter
”
由于 x
的两种情况, 在 myparameter
中使用,我认为我需要创建以下形式的委托:
Expression<Func<T, object>> expr = x => [myparameter expression here]
我认为这可行:
var extMethodExpr = expr.Body as MethodCallExpression;
var myparameterExpr = extMethodExpr.Arguments[1];
var myparam = Expression.Lambda(myparameterExpr, expr.Parameters).Compile().Invoke(someT)
但对于不涉及 x
, 的表达式我得到 TargetParameterCountException
:(
在这些情况下,如果我这样做:
var myparam = Expression.Lambda(myparameterExpr).Compile().Invoke(someT)
它工作正常。
我该如何解决这个问题?
谢谢
Update: This does work, I was being stupid :(
i have the following extension method
public static string ExtMethod(this object self, object myparameter);
at runtime this is called in any number of ways ways, i think these are all possibilities:
Expression<Func<T, string>> expr = x => x.property.ExtMethod(5);
Expression<Func<T, string>> expr = x => x.property.ExtMethod(new object());
Expression<Func<T, string>> expr = x => x.property.ExtMethod(someMethod());
Expression<Func<T, string>> expr = x => x.property.ExtMethod(x.someMethod());
Expression<Func<T, string>> expr = x => x.property.ExtMethod(x.OtherProperty);
what i need to do is evaluate the "myparameter
", given "expr
" and a "T
"
because of the two cases where x
is used in myparameter
, i thought i needed to create a delegate of the form:
Expression<Func<T, object>> expr = x => [myparameter expression here]
i thought this would work:
var extMethodExpr = expr.Body as MethodCallExpression;
var myparameterExpr = extMethodExpr.Arguments[1];
var myparam = Expression.Lambda(myparameterExpr, expr.Parameters).Compile().Invoke(someT)
but for the expressions that do not involve x
, i get TargetParameterCountException
:(
in these cases, if i do:
var myparam = Expression.Lambda(myparameterExpr).Compile().Invoke(someT)
it works fine.
How do I solve this?
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的; 追根究底; 行中:
如果您不尝试传入
someT
,这适用于参数中不涉及x
的表达式; 对于那些这样做的人,您需要告诉 lambda 包含该参数(与原始 lambda 相同的参数) - 简单地通过:这是一些计算内部参数的工作代码(给定参数类型的实例); 请注意,即使它不涉及
x
,我也会使用该参数 - 否则,它会对实例做什么?OK; got to the bottom of it; in the line:
If you weren't trying to pass in a
someT
, this would work for those expressions that don't involvex
in the argument; for those that do, you need to tell the lambda to include the parameter (the same one from the original lambda) - simply by:Here's some working code that evaluates the inner parameter (given an instance of the argument type); note that I use the parameter even if it doesn't involve an
x
- otherwise, what would it do with the instance?如果您检查 expr.Parameters.Count 并且如果它为 0,则不带参数调用会怎样?
What if you check expr.Parameters.Count and if it's 0, invoke without the parameters?