I use a WebBrowser object from WPF and I\'m calling some Javascript code in the page loaded in the browser like this:
myWebBrowser.InvokeScript(\"myJsFunc\", new
In case you really want to squeeze every bit of performance out of the code you can avoid the deserialization by means of eval inside javascript. The concept is to structure the call like this:
((IHTMLWindow2)webBrowserControl.Document.Window.DomWindow).execScript("var returnValue = someFunction([ 'abc', 'xyz', '1', '2', '3' ], { foo: 'xyz', bar: 1 });"
Notice that we use .execScript which makes all the difference in the world. This is because contrary to .InvokeScript which would forcefeed the javascript method with string based arguments (which is what forces you to use eval on the javascript side), .execScript() gives us the ability to write arbitrary javascript code including what you see above (notice that the arguments are an explicit javascript array and a bag of properties). Now we can directly code arrays and objects and write them as arguments. In order to do that we just use Newtonsoft.Json to serialize arrays and objects:
class Test {
public string foo;
public int bar;
}
((IHTMLWindow2)webBrowserControl.Document.Window.DomWindow).execScript("var returnValue = someFunction(" +
JsonConvert.SerializeObject((new List
Another thing is retrieving the resulting returned value:
string result = (string)webBrowserControl.Document.InvokeScript("eval", new object[] { @"returnValue;" }));
For your convenience you might want to write a utility method which iterates over the given parameters and serializes them properly, invokes the function and then retrieves the returned value.