Taritsyn / JavaScriptEngineSwitcher

JavaScript Engine Switcher determines unified interface for access to the basic features of popular JavaScript engines. This library allows you to quickly and easily switch to using of another JavaScript engine.
Apache License 2.0
439 stars 49 forks source link

How to pass c# objects to JavaScript functions? #96

Closed CeSun closed 2 years ago

CeSun commented 2 years ago

image

Taritsyn commented 2 years ago

Hello!

This cannot be done.

You can use the Execute or Evaluate method of JS engine with JSON serialization:

var myObj = new Obj();
string serializedObj = JsonConvert.SerializeObject(myObj);
jsEngine.Execute("fun(" + serializedObj + ");");

If you need an active object, then you can embed it:

var myObj = new Obj();
jsEngine.EmbedHostObject("myObj", myObj);
jsEngine.Execute("fun(myObj);");
CeSun commented 2 years ago

Thank you!