[英]How do I evaluate a boolean expression given as a String in X++ [AX 2012]
I have a scenario, in which I want to evaluate a boolean expression given as a string.我有一个场景,我想评估一个以字符串形式给出的布尔表达式。
For example, "1==4" when passed to some function would evaluate-to/return FALSE.例如,“1==4”在传递给某个函数时会评估为/返回 FALSE。
Similarly, "1==1" when passed to the same function would evaluate-to TRUE.类似地,“1==1”在传递给同一个函数时将评估为 TRUE。
If this can be done, then I would definitely want some way in which I can evaluate a dynamic boolean expression.如果可以这样做,那么我肯定会想要某种方式来评估动态布尔表达式。 For example, "workHours==4" is the the string expression and workHours is a variable.
例如,“workHours==4”是字符串表达式,workHours 是一个变量。
Let me know of any solution if you know.如果您知道,请告诉我任何解决方案。 Thanks.
谢谢。
There are ways to evaluate X++ expressions from text via .NET (with XLNT Framework, for example) and via some intrinsic functions in X++.有多种方法可以通过 .NET(例如,使用 XLNT 框架)和 X++ 中的一些内部函数从文本中评估 X++ 表达式。 But I'm pretty sure you can get to the same solution with a more clean aproach.
但是我很确定您可以通过更干净的方法获得相同的解决方案。 For example, you can pass a container to a function and then compare their values, or something like that.
例如,您可以将一个容器传递给一个函数,然后比较它们的值,或类似的东西。
If you still want to go that way, take a look to the runbuf
function in X++.如果您仍然想这样做,请查看 X++ 中的
runbuf
函数。
http://msdn.microsoft.com/en-us/library/aa656300.aspx http://msdn.microsoft.com/en-us/library/aa656300.aspx
Self learned the answer:自学答案:
Here's the code sample, how you can do it.这是代码示例,您可以如何操作。
static void Job22(Args _args)
{
str expr = 'ab==2';
str method = @'boolean eval(int ab){return ' + expr + ';}';
boolean result;
;
result = runBuf(method, 5);
info (strfmt("Calculation result is %1", result));
}
This might be a better example for your use where you can easily just change expression
.这可能是您使用的更好示例,您可以轻松地更改
expression
。
static void RunBufExample()
{
str expression = '2==1';
str func = 'boolean myFunc() {return ' + expression + ';}';
boolean retVal;
new ExecutePermission().assert();
retVal = runBuf(func);
CodeAccessPermission::revertAssert();
info(strFmt("%1", retVal));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.