【问题标题】:How do I print complete element arrays in Jint - like you can with Javascript?如何在 Jint 中打印完整的元素数组 - 就像使用 Javascript 一样?
【发布时间】:2019-04-02 17:15:39
【问题描述】:

我正在尝试通过Jint 访问Javascript array。我们基本上接受自定义javascript 代码在一个跨各种平台工作的应用程序中,并使用Jint 代替.NET。我们需要相同的结果 - 无论是 .NET 还是原版 JavaScript

        private Engine _engine = new Engine();
        var testList = new List<int> { 1, 2, 3, 4, 5 };
        _engine.SetValue("testList", testList);
        var result = _engine.Execute("'Elements of the array are: ' + testList").GetCompletionValue();
        var finalVal = result.AsString();

我希望结果是:

Elements of the array are: 1,2,3,4,5(类似于香草JavaScript

但是,它会引发异常。

System.InvalidOperationException: No matching indexer found.

请注意,如果我尝试仅访问数组的一个元素(例如 testList[1]),它会正常工作。

我在这里做错了吗?如果没有,那么我可以实现一个自定义索引器来完成这项工作吗?

【问题讨论】:

    标签: javascript c# .net jint


    【解决方案1】:

    Array.from()包围你的testList。见下面的例子

        private Engine _engine = new Engine();
        var testList = new List<int> { 1, 2, 3, 4, 5 };
        _engine.SetValue("testList", testList);
        var result = _engine.Execute("'Elements of the array are: ' + Array.from(testList)").GetCompletionValue();
        var finalVal = result.AsString();
    

    【讨论】:

      猜你喜欢
      • 2019-03-12
      • 2011-02-07
      • 2020-04-09
      • 2015-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-10
      • 2010-10-01
      相关资源
      最近更新 更多