Replies: 1 comment
-
using Jint;
using Jint.Native;
var engine = new Engine()
.SetValue("log", new Action<object>(Console.WriteLine));
// ----------------------------
// myObject.myProperty = 3;
engine.Execute(@"var myObject = {}");
var myObject = (JsObject)engine.GetValue("myObject");
myObject.Set("myProperty", 3);
// or
myObject.Set("myProperty", new JsNumber(3));
Console.WriteLine("Expected: 3");
engine.Execute(@"log(myObject.myProperty);");
// ----------------------------
// noExecuteCall = { myProperty: "Hello World" };
var noExecuteCall = new JsObject(engine);
noExecuteCall.Set("myProperty", new JsString("Hello World"));
engine.SetValue("noExecuteCall", noExecuteCall);
Console.WriteLine("Expected: Hello World");
engine.Execute(@"log(noExecuteCall.myProperty);");
// ----------------------------
var doesNotExist = engine.GetValue("doesNotExist");
Console.WriteLine(doesNotExist);
var casting = (JsObject)doesNotExist; Expected: 3
3
Expected: Hello World
Hello World
undefined
Unhandled exception. System.InvalidCastException: Unable to cast object of type 'Jint.Native.JsUndefined' to type 'Jint.Native.JsObject'. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Sorry if this has been asked before. I did try searching.
So
engine.SetValue()
allows you to set the value of a global variable in the js environment from C#. Is there a convenient way to set a property of a js object through C#? I was hoping thatSetValue()
could take a path in the name, likeengine.SetValue("myObject.myProperty", 3);
. However, experimentation has proven that this doesn't work.This functionality would be very convenient as, at the moment, the best solution I've come up with is this:
This is unfortunate for a few reasons:
SetValue()
andExecute()
Thanks for any thoughts on this.
Beta Was this translation helpful? Give feedback.
All reactions