我正在尝试使用 NCalc 从 JSON 文件中解析一些公式。
但是有时我需要通过 GUID 引用另一个对象并获取该对象公式:
public class FooObject
{
NCalc.Expression expression;
double Value;
public void SetValue()
{Value = (double)expression.Evaluate()} //this throws "Value was either too large or too small for a Double."
public FooObject()
{ expression = new NCalc.Expression("TechData(b8ef73c7-2ef0-445e-8461-1e0508958a0e)")}
expression.EvaluateFunction += NCalcFunctions;
}
}
public static void NCalcFunctions(string name, FunctionArgs args)
{
if (name == "TechData") //breakpoint shows we never get this far
{
Guid techGuid = new Guid(args.Parameters[0].ToString());
TechSD techSD = _staticData.Techs[techGuid]; //this returns an TechSD object that matches the given guid.
args.Result = techSD.DataExpression;//returns an NCalc expression from the techSD
}
}
SetValue()
引发异常(对于 Double 而言 Value 太大或太小),并且永远不会调用自定义函数。
我怀疑它正在尝试解析 GUID。这是正确的吗?我试图用 NCalc 做的事情是可能的吗?有更好的工具吗?