如何从已编译的 LambdaExpression 中获取计算值?

发布于 2024-11-04 09:18:27 字数 689 浏览 1 评论 0原文

我目前正在从事一个项目,我们正在使用 Telerik RadControls for Silverlight。在 2011 年第一季度发布的版本中,他们添加了一个名为 Rad Expression Editor 的新控件。在Rad表达式编辑器中,您可以传入对象并创建公式(或表达式),编辑器窗口将为您提供表达式结果的预览。我已经与 Telerik 讨论过这个问题,他们故意没有公开结果,但提到我可以使用 LambdaExpression.Compile()。我对 Linq 和 Lambda 表达式的使用非常陌生,但我开始对此进行研究。

举个例子,假设我有一个名为 Finances 的对象,在该对象中,有 4 个可为空的十进制字段(值):Debit (10)、DebitYTD (100)、Credit (20) 和 CreditYTD (200)。在公式中,我想要执行以下操作:借方 - 贷方 + DebitYTD - CreditYTD。

Telerik Rad 表达式编辑器将为我提供生成的表达式: ExpressionEditor.Expression = {Param_0 =>; ((Param_0.Debit - Param_0.Credit + Param_0.DebitYTD - Param_0.CreditYTD}

该表达式的结果应该是 -110。我需要能够获取表达式中计算的值,但是有无法弄清楚如何获得这个号码。任何人都可以解释一下如何实现这一点吗?谢谢。

I'm currently working on a project at work, and we are using the Telerik RadControls for Silverlight. In their Q1 2011 release, they added a new control called the Rad Expression Editor. In the Rad Expression Editor, you can pass in an object and create formulas (or expressions), and the editor window will give you a preview of the result of the expression. I've spoken with Telerik about this and they intentionally did not expose the result of this, but mentioned that I could use LambdaExpression.Compile(). I'm very new to Linq and using Lambda Expressions in general, but started looking into this.

As an example, lets say I have an object called Finances, and in that object, there are 4 nullable decimal fields (values): Debit (10), DebitYTD (100), Credit (20) and CreditYTD (200). In the formula, I want to do something like: Debit - Credit + DebitYTD - CreditYTD.

The Telerik Rad Expression Editor will give me the expression that is generated:
ExpressionEditor.Expression = {Param_0 => ((Param_0.Debit - Param_0.Credit + Param_0.DebitYTD - Param_0.CreditYTD}

The result of this expression should be -110. I need to be able to get the value that is calculate in the expression, but have not been able to figure out how to get this number. Can anyone please explain how this can be accomplished? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

错々过的事 2024-11-11 09:18:27

您并没有真正告诉我们太多关于它公开的 API,但它听起来就像您可以使用:

var compiled = ExpressionEditor.Expression.Compile();
var result = compiled(input);

其中 inputFinances< 类型的适当变量/代码>。

编辑:好的,因为表达式没有很好地暴露:

var typeSafe = (Expression<Func<Finance, decimal?>>) ExpressionEditor.Expression;
var compiled = typeSafe.Compile();
var result = compiled(input);

You haven't really told us much about the API which it exposes, but it sounds like you could use:

var compiled = ExpressionEditor.Expression.Compile();
var result = compiled(input);

where input is an appropriate variable of type Finances.

EDIT: Okay, as the expression isn't exposed nicely:

var typeSafe = (Expression<Func<Finance, decimal?>>) ExpressionEditor.Expression;
var compiled = typeSafe.Compile();
var result = compiled(input);
眼趣 2024-11-11 09:18:27

我有非常相似的要求。需要编译表达式并在运行时针对不同实例进行评估。然而,函数编译并不是那么简单,因为您必须在编译时知道结果值的类型,并且在大多数情况下这是不可能的:用户写入“1+1”,您得到 System.Int32,或者用户写入“ (1+1).ToString()”,您将得到 System.String。我确信这就是你经历(或将会经历)困难的时刻。

为了解决表达式编译问题,我建议跳入 DLR(动态语言运行时)。您将需要为您的 Silverlight 项目引用“Microsoft.CSharp”程序集。由于您只需要访问表达式的“编译”方法(已经存在于其中),因此针对“动态”对象执行此操作是足够公平的。下面是一个简短的示例来演示:

  dynamic dynamicExpression = expressionEditor.Expression;
  dynamic compiledExpression = dynamicExpression.Compile();
  object executionResult = compiledExpression(myInstance);

“executionResult”变量将保存表达式求值的结果。现在您可以对结果执行任何您需要执行的操作。

希望有帮助

I had very similar requirements. There was a need to compile expression and evaluate at runtime against different instances. However function compilation is not so trivial as you have to know the type of the result value during compile time and in most of the cases this will be impossible: user writes "1+1" and you get System.Int32, or user writes "(1+1).ToString()" and you get System.String. I'm sure this is the point where you experience (or will experience) difficulties.

In order to solve the expression compilation problem I would recommend jumping into DLR (Dynamic Language Runtime). You will need to reference "Microsoft.CSharp" assembly for your Silverlight project. As you need just access to "Compile" method for the expression (that is already in there), it will be fair enough doing that against "dynamic" object. Here's a short sample to demonstrate that:

  dynamic dynamicExpression = expressionEditor.Expression;
  dynamic compiledExpression = dynamicExpression.Compile();
  object executionResult = compiledExpression(myInstance);

The "executionResult" variable will hold the result of expression evaluation. Now you can do whatever you need to do with the result.

Hope that helps

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