Quantcast
Channel: ClearScript
Viewing all articles
Browse latest Browse all 2297

New Post: How to call function by name with arguments as array

$
0
0
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:
publicstaticclass ScriptEngineExtensions
{
    publicstaticobject 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", newobject[] { 1, 2, 3 });
Cheers!

Viewing all articles
Browse latest Browse all 2297

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>