如何使用Jint运行Js代码时使用GetValues?

时间:2015-03-13 14:36:30

标签: javascript jint jurassic

我的问题是原始的。但它非常有用。我在Sebastien Ros上查看了GitHub Jint应用程序。

我怎样才能多次使用GetValue属性?

GetValues(x,y,z...)GetValue("x").GetValue("y").GetValue("z")。因为我需要x,y,z ..结果。不仅是x值。

我有一个项目,我必须运行js代码,但有很多比较(小心它不是“if-else”,有if-if-if并继续......)我有权访问对所有if语句结果。我发现有GetValue方法。但我只能访问一个值。当我想访问“y”时,我必须使用GetValue("y")。但我想在同一时间看到“x”值。也许我希望GetValues("x","y","z"...)

            var square = new Engine()
           .SetValue("x", 3) // define a new variable
           .SetValue("y",4)
           .Execute(" var isok1=false;  var isok2= false; if(3>1) { x * x; isok1=true; } if(2>1) { y * y }").GetValue("y") // execute a statement
           .ToObject() // converts the value to .NET
           ;
            Console.WriteLine(square.ToString());

      var square = new Engine()
           .SetValue("x", 3) // define a new variable
           .SetValue("y",4)
           .Execute(" var isok1=false;  var isok2= false; if(3>1) { x * x; isok1=true; } if(2>1) { y * y }      isok1;").GetCompletionValue() // execute a statement
           .ToObject() // converts the value to .NET
           ;
            Console.WriteLine(square.ToString());

我查看了Jurassic on codeplex

我用它如下:

   var engine = new Jurassic.ScriptEngine();
            engine.SetGlobalValue("x", 15);
            engine.SetGlobalValue("y", 2);
          
            engine.Execute(@" var isok1=false;  var isok2= false; if(3>1) { x=x * x; isok1=true; } if(2>1) { y= y * y; isok2=true; } ");
            Console.WriteLine(engine.GetGlobalValue<int>("x"));
            Console.WriteLine(engine.GetGlobalValue<int>("y"));
            Console.WriteLine(engine.GetGlobalValue<bool>("isok1"));
            Console.WriteLine(engine.GetGlobalValue<bool>("isok2"));
            Console.ReadKey();

问题:

我如何在myproject中做到这一点但是使用Jint而不是Jurassic?我需要Jint multiGetValues属性......

1 个答案:

答案 0 :(得分:1)

您可以从JavaScript返回一个数组: var result = (object[])engine.Execute("[x, y, z]").GetCompletionValue().ToObject(); 或动态对象 dynamic result = engine.Execute("{x, y, z}").GetCompletionValue().ToObject(); Console.WriteLine(result.x);