简体   繁体   English

如何使用Jint从JavaScript函数调用中获取适当的C#字符串数组

[英]How to obtain a proper C# string array from a JavaScript function call using Jint

I'm using Jint to evaluate JavaScript within a C# application. 我正在使用Jint在C#应用程序中评估JavaScript。 My JS returns an array of strings: 我的JS返回一个字符串数组:

return ["blah", "blah", "blah"];

But I'm not sure how to properly use that array once I'm back in C#: 但是我不确定回到C#后如何正确使用该数组:

Object result = e.Execute (javaScript).Invoke("jsFunction", answers).ToObject();
string[] resultAsStrings = ???;

Here's what the debugger looks like: 这是调试器的外观:

在此处输入图片说明

You can just cast the result of ToObject() to an object[] in this case. 在这种情况下,您可以将ToObject()的结果ToObject()转换为object[]

Here are the transformations that ToObject() applies when facing a JavaScript value: 以下是当面对JavaScript值时ToObject()应用的转换:

  • undefined : null undefinednull
  • null : null nullnull
  • Boolean : bool Booleanbool
  • String : string Stringstring
  • Number : double Numberdouble
  • Array : object[] with each element itself converted using ToObject() Arrayobject[] ,每个元素本身使用ToObject()转换
  • Date : DateTime DateDateTime
  • Function : Func<JsValue, JsValue[], JsValue> FunctionFunc<JsValue, JsValue[], JsValue>
  • RegExp : Regex RegExp :正则Regex
  • Object : dynamic with all the properties of the object converted using ToObject() Objectdynamic具有使用ToObject()转换的对象的所有属性

If no type is matching, it throws ArgumentOutOfRangeException 如果没有匹配的类型,则抛出ArgumentOutOfRangeException

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM