JavaScript Side
<script type="text/javascript"> // get .net object public field from js var myFunction1 = function (a) { document.title = a.IntTest + a.StringTest + a.ArrayTest[0] + a.JaggedArrayTest[1][1] + a.DictionaryTest[1]; }; // invoke .net object public methods from js var myFunction2 = function (a) { var map = new Map([[1, "val1"], [2, "val2"]]); a.Init(1, 1.1, "test", '*', [1.2, 1.3], [[1, 2, 3], [4, 5]], Array.from(map)); }; </script>
.NET Side
ExampleObj class:
C#
private class ExampleObj { public event EventHandler DataInit; public int IntTest { get; set; } public decimal DecimalTest { get; set; } public string StringTest { get; set; } public char CharTest { get; set; } public double[] ArrayTest { get; set; } public int[][] JaggedArrayTest { get; set; } public IDictionary<int, string> DictionaryTest { get; set; } public void Init(int intTest, decimal decimalTest, string stringTest, char charTest, double[] arrayTest, int[][] jaggedArrayTest, IDictionary<int, string> dictionaryTest) { this.IntTest = intTest; this.DecimalTest = decimalTest; this.StringTest = stringTest; this.CharTest = charTest; this.ArrayTest = arrayTest; this.JaggedArrayTest = jaggedArrayTest; this.DictionaryTest = dictionaryTest; var dataInit = DataInit; if (dataInit != null) { dataInit.Invoke(this, new EventArgs()); }
VB.NET
Private Class ExampleObj Public Event DataInit As EventHandler Public Property IntTest As Integer Public Property DecimalTest As Decimal Public Property StringTest As String Public Property CharTest As Char Public Property ArrayTest As Double() Public Property JaggedArrayTest As Integer()() Public Property DictionaryTest As IDictionary(Of Integer, String) Public Sub Init(ByVal intTest As Integer, ByVal decimalTest As Decimal, ByVal stringTest As String, ByVal charTest As Char, ByVal arrayTest As Double(), ByVal jaggedArrayTest As Integer()(), ByVal dictionaryTest As IDictionary(Of Integer, String)) Me.IntTest = intTest Me.DecimalTest = decimalTest Me.StringTest = stringTest Me.CharTest = charTest Me.ArrayTest = arrayTest Me.JaggedArrayTest = jaggedArrayTest Me.DictionaryTest = dictionaryTest Dim dataInit = DataInitEvent If dataInit IsNot Nothing Then dataInit.Invoke(Me, New EventArgs()) End If
Calling myFunction1()
C#
// call myFunction1 { String title = String.Empty; browser.TitleChangedEvent += delegate(object sender, TitleEventArgs e) { title = e.Title; Console.WriteLine("Title: {0}", title); }; JSValue value = browser.ExecuteJavaScriptAndReturnValue("myFunction1"); ExampleObj a = new ExampleObj(); a.IntTest = 1; a.StringTest = "z"; a.ArrayTest = new double[] { 1, 2 }; int[][] jaggedArrayTest = { new int[] {1}, new int[] {2,3} }; a.JaggedArrayTest = jaggedArrayTest; a.DictionaryTest = new Dictionary<int, string>(); a.DictionaryTest.Add(1, "a"); ((JSFunction)value).Invoke(null, a); /*Output: Title: 1z13a */ }
VB.NET
Sub myFunction1() Dim title As String = String.Empty AddHandler browser.TitleChangedEvent, sub(ByVal sender As Object, ByVal e As TitleEventArgs) title = e.Title Console.WriteLine("Title: {0}", title) End sub Dim value As JSValue = browser.ExecuteJavaScriptAndReturnValue("myFunction1") Dim a As ExampleObj = New ExampleObj() a.IntTest = 1 a.StringTest = "z" a.ArrayTest = New Double() {1, 2} Dim jaggedArrayTest As Integer()() = {New Integer() {1}, New Integer() {2, 3}} a.JaggedArrayTest = jaggedArrayTest a.DictionaryTest = New Dictionary(Of Integer, String)() a.DictionaryTest.Add(1, "a") (CType(value, JSFunction)).Invoke(Nothing, a) 'Output: Title: 1z13a End Sub
Calling myFunction2()
C#
{ JSValue value = browser.ExecuteJavaScriptAndReturnValue("myFunction2"); var a = new ExampleObj(); a.DataInit += delegate { Console.WriteLine("IntTest = {0}, DecimalTest = {1}, StringTest = {2}, CharTest = {3}, ArrayTest[0] = {4}, JaggedArrayTest[1][1] = {5}, DictionaryTest[2] = {6}" , a.IntTest, a.DecimalTest, a.StringTest, a.CharTest, a.ArrayTest[0], a.JaggedArrayTest[1][1], a.DictionaryTest[2]); }; ((JSFunction)value).Invoke(null, a); /*Result: IntTest = 1, DecimalTest = 1.1, StringTest = test, CharTest = *, ArrayTest[0] = 1.2, JaggedArrayTest[1][1] = 5, DictionaryTest[2] = val2 */ }
VB.NET
Sub myFunction1() Dim value As JSValue = browser.ExecuteJavaScriptAndReturnValue("myFunction2") Dim a = New ExampleObj() AddHandler a.DataInit, sub() Console.WriteLine("IntTest = {0}, DecimalTest = {1}, StringTest = {2}, CharTest = {3}, ArrayTest[0] = {4}, JaggedArrayTest[1][1] = {5}, DictionaryTest[2] = {6}", a.IntTest, a.DecimalTest, a.StringTest, a.CharTest, a.ArrayTest(0), a.JaggedArrayTest(1)(1), a.DictionaryTest(2)) End sub (CType(value, JSFunction)).Invoke(Nothing, a) 'Result: ' IntTest = 1, DecimalTest = 1.1, ' StringTest = test, CharTest = *, ' ArrayTest[0] = 1.2, JaggedArrayTest[1][1] = 5, ' DictionaryTest[2] = val2 End Sub