nCalcExpression.EvaluateFunction += removes support of the pre-existing if() function
For example:
var nCalcExpression = GetNCalcExpression("if(1<2,'true','false')");
nCalcExpression.EvaluateFunction += NCalcExtensions.NCalcExtensionFunctions;
var resultObject = nCalcExpression.Evaluate();
var resultString = resultObject.ToString();
This will result in an exception, commenting out the second line would not.
The workaround is to add the following to your static NCalcExtensions.NCalcExtensionFunctions method:
public static void NCalcExtensionFunctions(string functionName, FunctionArgs functionArgs)
{
string param1;
string param2;
switch (functionName)
{
case "if":
bool boolParam1;
object objectParam1;
object objectParam2;
try
{
boolParam1 = (bool)functionArgs.Parameters[0].Evaluate();
objectParam1 = functionArgs.Parameters[1].Evaluate();
objectParam2 = functionArgs.Parameters[2].Evaluate();
}
catch (Exception)
{
throw new MacroParameterException(FunctionConst, "if() requires three parameters.");
}
functionArgs.Result = boolParam1 ? objectParam1 : objectParam2;
return;
... other cases
}
}
For example:
var nCalcExpression = GetNCalcExpression("if(1<2,'true','false')");
nCalcExpression.EvaluateFunction += NCalcExtensions.NCalcExtensionFunctions;
var resultObject = nCalcExpression.Evaluate();
var resultString = resultObject.ToString();
This will result in an exception, commenting out the second line would not.
The workaround is to add the following to your static NCalcExtensions.NCalcExtensionFunctions method:
public static void NCalcExtensionFunctions(string functionName, FunctionArgs functionArgs)
{
string param1;
string param2;
switch (functionName)
{
case "if":
bool boolParam1;
object objectParam1;
object objectParam2;
try
{
boolParam1 = (bool)functionArgs.Parameters[0].Evaluate();
objectParam1 = functionArgs.Parameters[1].Evaluate();
objectParam2 = functionArgs.Parameters[2].Evaluate();
}
catch (Exception)
{
throw new MacroParameterException(FunctionConst, "if() requires three parameters.");
}
functionArgs.Result = boolParam1 ? objectParam1 : objectParam2;
return;
... other cases
}
}