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

New Post: Passing javascript objects to c# native code

0
0
So the "static" console.logs alternative to dynamic only slightly cheaper.

Right, but that's after you minimized your usage of the dynamic infrastructure. In your original implementation you declared o as a dynamic parameter, which led to unnecessary dynamic calls to GetDynamicMemberNames() and ToString(). You also redundantly evaluated o[egenskap] several times; each of those indexing operations is a dynamic call into the underlying script engine.

​​I think, however, there is one weakness in GetDynamicMemberNames: that you don't know if its an array or object. Is there another way of knowing?

For arrays, GetDynamicMemberNames() returns the valid indices as strings. If that's not what you're looking for, remember that examining script objects is often easiest to do in script code:
dynamic isScriptArray = engine.Evaluate("(function (x) { return x instanceof Array; })");

var result = engine.Evaluate("[1,2,3,4,5]");
if (isScriptArray(result)) {
    // do something
}
Cheers!

Viewing all articles
Browse latest Browse all 2297