user1322801
user1322801

Reputation: 859

Communication between Jint and JavaScript

I am looking for a way to communicate (back and forth) between Jint and C#.

Is there a way? I have no problem of running JavaScripts in Jint after loading them to the engine but I still have an issue getting the callbacks on the other hand - from the JavaScript back to C#(maybe using some kind of ObjectForScripting? or other predefined settings?) Thanks

Upvotes: 4

Views: 900

Answers (1)

Chet
Chet

Reputation: 3729

In C#, provide a class with a method you want to run.

public class JavaScriptHelper {
    public string Method(string input) {
        return String.Concat("Hi", input);
    }
}

Then pass the class to the engine.

var engine = new Engine();
engine.SetValue("helper", new JavaScriptHelper());
var source = @" var result = helper.Method('Hello');"
engine.Run(source);

Upvotes: 1

Related Questions