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

New Post: Passing javascript objects to c# native code

0
0
I am currently trying to pass a js array to a C# method.


Engine.AddHostObject("plotUtility", plotUtility);

Engine.Evaluate("myData = [ [1,2], [3,4],[5,8] ]" );
....
Engine.Evaluate("plotUtility.plot( myData )");

Where plotUtility is a c# object with method plot.

What would be the proper method for converting myData to a C# array? It comes in as a Microsoft.ClearScript.V8.V8ScriptItem which is protected, meaning i can't use it it my PlotUtlity.Plot signature. (e.g. Public void Plot(V8ScriptItem scriptItem) does not work.)

I can do the following:

Engine.Evaluate("plotUtility.plot( JSON.stringify(myData ) )");

and use a c# signature: Public void Plot(string scriptItem), but that seems a bit superfluous.

Is there a better way to do this where i can preserve the syntax:

Engine.Evaluate("plotUtility.plot( myData )");

Viewing all articles
Browse latest Browse all 2297