How to call function by name with arguments as array

Mar 4, 2014 at 3:33 PM
How to call function by name with arguments as array.
Something like this _engine.Execute("sum", new []{ 2, 4});
Coordinator
Mar 4, 2014 at 7:17 PM
Hello ifle,

Currently ClearScript doesn't have an API method like that. We'll add one in the next release. In the meantime, you can add an extension method that does what you want:
public static class ScriptEngineExtensions
{
    public static object InvokeFunction(this ScriptEngine engine, string name, object[] args)
    {
        var host = new HostFunctions();
        ((IScriptableObject)host).OnExposedToScriptCode(engine);
        var del = (Delegate)host.func<object>(args.Length, engine.Script[name]);
        return del.DynamicInvoke(args);
    }
}
With this class in place, you can do this:
engine.InvokeFunction("foo", new object[] { 1, 2, 3 });
Cheers!
Mar 4, 2014 at 8:45 PM
Thanks