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

New Post: Passing javascript objects to c# native code

$
0
0
Greetings!

ClearScript was designed to provide direct access to script objects, without data conversion. For example, you could write your C# class as follows:
publicclass ConsolePlotUtility {
    publicvoid plot(dynamic data) {
        for (var i = 0; i < data.length; i++)
            Console.WriteLine("({0},{1})", data[i][0], data[i][1]);
    }
}
and then use it like this:
engine.AddHostObject("plotUtility", new ConsolePlotUtility());
engine.Evaluate("plotUtility.plot([[1,2], [3,4], [5,8]])");
Keep in mind, however, that host-to-script and script-to-host calls are relatively expensive. Data conversion can actually yield better performance in many cases. With JSON, for example, you can package your data and move it across the boundary in one step, but of course you have to weigh that against the costs of JSON serialization and parsing.

Good luck, and thanks for your question!

Viewing all articles
Browse latest Browse all 2297

Trending Articles



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