I'm attempting to pass an object to a javascript function via jint and return a value. But it doesn't seem to work. Here is what I have tried so far -
Error -
Jint.Runtime.JavaScriptException: 'obj is undefined'
Using the following code -
var carObj = JsonSerializer.Serialize(car);
var engine = new Jint.Engine();
engine.SetValue("obj", carObj);
var value = engine
.Execute("const result = (function car(obj) { const type = obj.Type; return type;})()")
.GetValue("result");
As shown in the docs, you should pass your POCO
cardirectly toJint.Enginerather than trying to serialize it to JSON. Jint will use reflection to access its members.Thus your code can be rewritten as follows:
Or equivalently as:
Or
Here I am assuming that
caris a POCO that has a string propertyType, e.g.Demo fiddle here.