How to call a function written in javascript file with C # using IronJS

I just load Iron JS and after executing some 2/3 simple programs using the Execute method, I study the ExecuteFile method.

I have a Test.js file whose contents are equal

function Add(a,b)
{
    var result = a+b;
    return result;
}

I want to use the same from C # using Iron JS. How can i do this? My code is bye

var o = new IronJS.Hosting.CSharp.Context();
dynamic loadFile = o.ExecuteFile(@"d:\test.js");
var result = loadFile.Add(10, 20);

But the loadfile variable is null (the path is correct).

How to call JS function, please help ... Also google search did not give any help.

thank

+5
source share
1 answer

The result of the execution will be empty, since your script returns nothing.

"globals" script .

var o = new IronJS.Hosting.CSharp.Context();
o.ExecuteFile(@"d:\test.js");
dynamic globals = o.Globals;

var result = globals.Add(10, 20);

EDIT:. , , NuGet. , IronJS 0.2.0.1:

var o = new IronJS.Hosting.CSharp.Context();
o.ExecuteFile(@"d:\test.js");
var add = o.Globals.GetT<FunctionObject>("Add");

var result = add.Call(o.Globals, 10D, 20D).Unbox<double>();
+6

All Articles