将对象传递给 jint 中的函数并 return 一个值
Pass an object to a function in jint and return a value
我试图通过 jint 和 return 值将对象传递给 javascript 函数。但这似乎不起作用。这是我到目前为止尝试过的 -
错误-
Jint.Runtime.JavaScriptException: 'obj is undefined'
使用下面的代码-
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");
如 docs, you should pass your POCO car
directly to Jint.Engine
所示,而不是尝试将其序列化为 JSON。 Jint 将使用反射来访问它的成员。
因此您的代码可以重写如下:
var value = new Jint.Engine() // Create the Jint engine
.Execute("function car(obj) { const type = obj.Type; return type;}") // Define a function car() that accesses the Type field of the incoming obj and returns it.
.Invoke("car", car); // Invoke the car() function on the car POCO, and return its result.
或等效于:
var value = new Jint.Engine()
.SetValue("obj", car) // Define a "global" variable "obj"
.Execute("const result = (function car(obj) { const type = obj.Type; return type;})(obj)") // Define the car() function, call it with "obj", and set the value in "result"
.GetValue("result"); // Get the evaluated value of "result"
或
var value = new Jint.Engine() // Create the Jint engine
.SetValue("obj", car) // Define a "global" variable "obj"
.Execute("function car(obj) { const type = obj.Type; return type;}; car(obj);") // Define the car() function, and call it with "obj".
.GetCompletionValue(); // Get the last evaluated statement completion value
这里我假设 car
是一个具有字符串 属性 Type
的 POCO,例如
var car = new
{
Type = "studebaker convertible",
};
演示 fiddle here.
我试图通过 jint 和 return 值将对象传递给 javascript 函数。但这似乎不起作用。这是我到目前为止尝试过的 -
错误-
Jint.Runtime.JavaScriptException: 'obj is undefined'
使用下面的代码-
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");
如 docs, you should pass your POCO car
directly to Jint.Engine
所示,而不是尝试将其序列化为 JSON。 Jint 将使用反射来访问它的成员。
因此您的代码可以重写如下:
var value = new Jint.Engine() // Create the Jint engine
.Execute("function car(obj) { const type = obj.Type; return type;}") // Define a function car() that accesses the Type field of the incoming obj and returns it.
.Invoke("car", car); // Invoke the car() function on the car POCO, and return its result.
或等效于:
var value = new Jint.Engine()
.SetValue("obj", car) // Define a "global" variable "obj"
.Execute("const result = (function car(obj) { const type = obj.Type; return type;})(obj)") // Define the car() function, call it with "obj", and set the value in "result"
.GetValue("result"); // Get the evaluated value of "result"
或
var value = new Jint.Engine() // Create the Jint engine
.SetValue("obj", car) // Define a "global" variable "obj"
.Execute("function car(obj) { const type = obj.Type; return type;}; car(obj);") // Define the car() function, and call it with "obj".
.GetCompletionValue(); // Get the last evaluated statement completion value
这里我假设 car
是一个具有字符串 属性 Type
的 POCO,例如
var car = new
{
Type = "studebaker convertible",
};
演示 fiddle here.