DynamicExpresso.SetFunction 不使用方法重载

DynamicExpresso.SetFunction not working with method overload

我正在使用 Dynamic Expresso 进行表达式评估,效果非常好。实际上设置了自定义函数,但方法重载似乎有问题。

我实际上有两个 Round 方法:

第一个有一个参数的:

public static decimal Round(decimal userNumber)
{
    return Math.Round(userNumber);
}

第二个有两个参数:

public static decimal Round(decimal userNumber, int decimals)
{
    return Math.Round(userNumber, decimals);
}

已声明委托并且 Interpreter.SetFunction 也被调用且没有错误:

Func<decimal, decimal> roundFunction1 = (userNumber) => Round(userNumber);
Func<decimal, int, decimal> roundFunction2 = (userNumber, decimals) => Round(userNumber, decimals);

interpreter.SetFunction("ROUND", roundFunction1);
interpreter.SetFunction("ROUND", roundFunction2);

变量被正确传递,我已经多次检查我传递的参数实际上是 decimal.

我通过的评估表达式,最终包含以下字符串,这没有错,因为 Interpreter.SetVariable(property.Key, property.Value) 插入了一个 "ImporteTotal" 和一个 666.66:

ROUND(ImporteTotal)

因此,评估完成后我得到的异常是:

Argument list incompatible with delegate expression (at index 0).

请注意,我实际上设置了很多甚至使用 DateTimes、intstrings 等等的函数,重载为嗯!它们都工作得很好,但似乎删除任何这些重载都有助于它工作。如果它们都在 Dynamic Expresso 上 "imported",它会抱怨。

有什么想法吗?

非常感谢。

更新-编辑:

我查看了 UnitTesting Davide provided through pastebin,但似乎失败了。

这个单元测试似乎失败了。

[Test]
public static void TestInterpreter()
{
    var interpreter = new Interpreter();

    Func<decimal, decimal> roundFunction1 = (userNumber) => Round(userNumber);
    Func<decimal, int, decimal> roundFunction2 = (userNumber, decimals) => Round(userNumber, decimals);
    Func<string, string, int> charindexFunction1 = (toFind, userString) => Charindex(toFind, userString);
    Func<string, string, int, int> charindexFunction2 = (toFind, userString, offset) => Charindex(toFind, userString, offset);

    interpreter.SetFunction("ROUND", roundFunction1);
    interpreter.SetFunction("ROUND", roundFunction2);
    interpreter.SetFunction("CHARINDEX", charindexFunction1);
    interpreter.SetFunction("CHARINDEX", charindexFunction2);

    var importe = 666.66M;
    interpreter.SetVariable("ImporteTotal", importe);

    var textoAlbaran = "ALBARAN-01";
    interpreter.SetVariable("Albaran", textoAlbaran);

    Assert.AreEqual(Round(importe), interpreter.Eval("ROUND(ImporteTotal)"));
    Assert.AreEqual(Round(importe, 1), interpreter.Eval("ROUND(ImporteTotal, 1)"));
    Assert.AreEqual(Charindex("BARAN", textoAlbaran), interpreter.Eval("CHARINDEX(\"BARAN\", Albaran"));
    Assert.AreEqual(Charindex("BARAN", textoAlbaran, 2), interpreter.Eval("CHARINDEX(\"BARAN\", Albaran, 2)"));
}

public static decimal Round(decimal userNumber)
{
    return Math.Round(userNumber);
}

public static decimal Round(decimal userNumber, int decimals)
{
    return Math.Round(userNumber, decimals);
}

public static int Charindex(string toFind, string userString)
{
    return userString.IndexOf(toFind);
}

public static int Charindex(string toFind, string userString, int offset)
{
    return userString.IndexOf(toFind, offset);
}

DynamicExpresso 2.5.0 开始,可以注册多个具有相同名称的方法(即重载):

Func<decimal, decimal> roundFunction1 = (userNumber) => Math.Round(userNumber);
Func<decimal, int, decimal> roundFunction2 = (userNumber, decimals) => Math.Round(userNumber, decimals);

var interpreter = new Interpreter();
interpreter.SetFunction("ROUND", roundFunction1);
interpreter.SetFunction("ROUND", roundFunction2);

Assert.AreEqual(3.13M, interpreter.Eval("ROUND(3.12789M, 2)"));
Assert.AreEqual(3M, interpreter.Eval("ROUND(3.12789M)"));