JS - .NET Bridge API allows to inject .NET objects into JavaScript code. This is a powerful feature that allows to access .NET functionality from JavaScript.


Note: To access JavaScript and inject .NET object, make sure that web page is loaded completely and JavaScript support is enabled. If you inject/register .NET object and then load web page, all registered .NET objects will be destroyed and will not be available in JavaScript of the loaded web page.


Injecting .NET Objects

 

To inject .NET object into JavaScript you must associate the .NET object with specified property of a JavaScript object. For example, you can add a new property to global window JavaScript object (other objects would work as well) and associate this property with .NET object using the following code:

C#

JSValue window = browser.ExecuteJavaScriptAndReturnValue("window");
//Here, the CSharpObject can be substituted with any regular .NET object
window.AsObject().SetProperty("csharp", new CSharpObject());

VB.NET

Dim window As JSValue = browser.ExecuteJavaScriptAndReturnValue("window")
'Here, the CSharpObject can be substituted with any regular .NET object
window.AsObject().SetProperty("csharp", New CSharpObject())


Note: To inject .NET object into JavaScript code, we recommend that you use the following approach:

C#

browser.ScriptContextCreated += delegate(object sender, ScriptContextEventArgs e)
{
  JSValue value = browser.ExecuteJavaScriptAndReturnValue("window");
  value.AsObject().SetProperty("Account", new Account());
};

VB.NET

AddHandler browser.ScriptContextCreated, Sub(sender As object, e As ScriptContextEventArgs) 
    Dim value As JSValue = browser.ExecuteJavaScriptAndReturnValue("window")
    value.AsObject().SetProperty("Account", new Account())
End Sub


Implementation of .NET object can be the following:

C#

public class Account
{
  public void Save(String firstName, String lastName)
  {
    Console.Out.WriteLine("firstName = " + firstName);
    Console.Out.WriteLine("lastName = " + lastName);
  }
}

VB.NET

Public Class Account
    Public Sub Save(firstName As String, lastName As String)
        Console.Out.WriteLine("firstName = " + firstName)
        Console.Out.WriteLine("lastName = " + lastName)
    End Sub
End Class


Note: Make sure that you inject .NET object which class/interface is marked as public. Only public classes can be registered and accessed from JavaScript.


Now JavaScript code can invoke object public methods:


window.Account.Save("FirstName", "LastName");


Note: JavaScript code can access/invoke only public methods of registered .NET object.


How JavaScript calls .NET method


When JavaScript calls a public method of a registered .NET object, JavaScript parameters are automatically converted to corresponding .NET objects/primitive values by DotNetBrowser.


If DotNetBrowser cannot convert passed JavaScript values or find a method with the appropriate signature, JavaScript error will be thrown.


If registered .NET object has several methods with same name and number of parameters, but different parameter types, JavaScript will invoke the first method declared in .NET class.


JavaScript to .NET types conversion


JavaScript and .NET work with different primitive types. DotNetBrowser implements automatic types conversion from JavaScript to .NET types. Here's how JavaScript objects will be converted to their .NET equivalents by DotNetBrowser:


JavaScript.NET
Numberdouble, float, long, int, short, System.Double, System.Float System.Int64, System.Int32, System.Int16
Booleanbool, System.Boolean
StringSystem.String
nullnull
ObjectDotNetBrowser.JSObject
ArrayDotNetBrowser.JSArray
FunctionDotNetBrowser.JSFunction


Passing .NET objects as parameters for JSFunction.Invoke()


Starting from DotNetBrowser 1.8.2 the JSFunction.Invoke()method accepts .NET objects as parameters and passes them to the JavaScript side. In this case, it is possible to get or set the public field of the .NET object and invoke its public methods.