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

New Post: Returning Javascript arrays from HostObjects

$
0
0
Hi Chris,

First, consider upgrading to the latest version of ClearScript (currently 5.4.1). We recommend that you build it from the source code as outlined in the ReadMe.

Second, you've determined correctly that ClearScript prefers to make host objects scriptable rather than convert them to script objects. However, it also aims to make it easy to manipulate script objects from the host. Here's a sample that defines an extension method for converting host arrays to script arrays:
publicstaticclass ScriptHelpers {
    publicstaticobject ToScriptArray<T>(this T[] array) {
        dynamic scriptArray = ScriptEngine.Current.Evaluate("[]");
        foreach (var element in array) {
            scriptArray.push(element);
        }
        return scriptArray;
    }
}
Note that ScriptEngine.Current is new in ClearScript 5.4.1. With an older version you'd have to pass the engine instance into the conversion method (or use some other technique).

Good luck!

Viewing all articles
Browse latest Browse all 2297

Trending Articles



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