如何获取存储在 linq 调用表达式中的值
我扩展了 Dynamic.cs 以使其能够与索引器一起使用。在这种情况下,我需要获取存储在调用表达式中的索引。在调试器中,我可以看到:
- left {Param_0.get_Item(0)} System.Linq.Expressions.Expression {System.Linq.Expressions.InstanceMethodCallExpressionN}
- Arguments Count = 1 System.Collections.ObjectModel.ReadOnlyCollection<System.Linq.Expressions.Expression> {System.Runtime.CompilerServices.TrueReadOnlyCollection<System.Linq.Expressions.Expression>}
- [0] {0} System.Linq.Expressions.Expression {System.Linq.Expressions.ConstantExpression}
CanReduce false bool
DebugView "0" string
NodeType Constant System.Linq.Expressions.ExpressionType
+ Type {Name = "Int32" FullName = "System.Int32"} System.Type {System.RuntimeType}
Value 0 object {int}
+ Rohdatenansicht
我想要的是:
left.Arguments[0].Value
我怎样才能得到这个?
一个丑陋的方法可能是使用 left.ToString()。这将导致:“Param_0.get_Item(0)” 在那里我可以提取 get_Item() 的参数。但我认为这不是正确的方法。
I extend Dynamic.cs to qualify it for usage with indexers. In this scenario I need to get the index, which is stored in a call expression. In the debugger, I can see:
- left {Param_0.get_Item(0)} System.Linq.Expressions.Expression {System.Linq.Expressions.InstanceMethodCallExpressionN}
- Arguments Count = 1 System.Collections.ObjectModel.ReadOnlyCollection<System.Linq.Expressions.Expression> {System.Runtime.CompilerServices.TrueReadOnlyCollection<System.Linq.Expressions.Expression>}
- [0] {0} System.Linq.Expressions.Expression {System.Linq.Expressions.ConstantExpression}
CanReduce false bool
DebugView "0" string
NodeType Constant System.Linq.Expressions.ExpressionType
+ Type {Name = "Int32" FullName = "System.Int32"} System.Type {System.RuntimeType}
Value 0 object {int}
+ Rohdatenansicht
What I want is:
left.Arguments[0].Value
How can I get this?
An ugly method will be perhaps to use left.ToString(). This will result in: "Param_0.get_Item(0)" There I can extract the parameter of get_Item(). But I think this is not the right way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要获得该值,您只需将表达式转换为正确的类型:
或者,您可以使用
dynamic
:To get to that value, you just need to cast the expressions to the right types:
Alternatively, you can use
dynamic
: