如何获取存储在 linq 调用表达式中的值

发布于 2025-01-05 22:41:38 字数 1068 浏览 3 评论 0原文

我扩展了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感情旳空白 2025-01-12 22:41:38

要获得该值,您只需将表达式转换为正确的类型:

Expression left = …

var methodCall = (MethodCallExpression)left;

var constant = (ConstantExpression)methodCall.Arguments[0];

int value = (int)constant.Value;

或者,您可以使用dynamic

dynamic dynamicLeft = left;

int value = dynamicLeft.Arguments[0].Value;

To get to that value, you just need to cast the expressions to the right types:

Expression left = …

var methodCall = (MethodCallExpression)left;

var constant = (ConstantExpression)methodCall.Arguments[0];

int value = (int)constant.Value;

Alternatively, you can use dynamic:

dynamic dynamicLeft = left;

int value = dynamicLeft.Arguments[0].Value;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文