[英]How to run a jscript using C# and return the values in an array?
Well, I have a lot of variables in javascript that I need get the values(that I getting from other page). 好吧,我在javascript中有很多变量,我需要获取值(我从其他页面获取)。 What's the best way to do this?
最好的方法是什么? I'm using the
Microsoft.Jscript
class and your methods for it work. 我正在使用
Microsoft.Jscript
类和你的方法。
I written the following code: 我写了以下代码:
static Dictionary<string, string> ParseVariables(string code)
{
string[] variables = code.Split(';');
Dictionary<string, string> variablesValues = new Dictionary<string, string>();
for (int i = 0, len = variables.Length - 1; i < len; i++)
{
string vvar = variables[i];
string varName = Regex.Replace(vvar.Split('=')[0], @"^\s*var\s*", string.Empty).Trim();
var compiler = Compile(vvar);
string varValue = compiler.ToString();
variablesValues.Add(varName, varValue);
}
return variablesValues;
}
static object Compile(string JSource)
{
return Microsoft.JScript.Eval.JScriptEvaluate(JSource, Microsoft.JScript.Vsa.VsaEngine.CreateEngine());
}
This works fine to some cases,like: 这适用于某些情况,例如:
var jscript = "var x = 'foo'; var y = 'baa';";
var output = ParseVariables(jscript);
var val = output["y"]; //baa
var val2 = output["x"]; //foo
but to cases like this: 但对于这样的情况:
var jscript = "var x = {}; x['foo'] = 'baa'; x['baa'] = 'test';";
var output = ParseVariables(jscript);
var val = output["x['foo']"]; //not works
How I do this? 我是怎么做到的 Any help is appreciated!
任何帮助表示赞赏! Thanks!
谢谢!
Since your approach is to split the JScript source code in chunks separated by semicolon (;), only the part between var
and ;
由于您的方法是将JScript源代码拆分为以分号(;)分隔的块,因此只有
var
和;
之间的部分;
will be compiled using your Compile method. 将使用您的编译方法编译。
If you change your JScript source code into var x = { "foo": "baa", "baa": "test" };
如果将JScript源代码更改为
var x = { "foo": "baa", "baa": "test" };
, the Compile method will work properly, and it will return a ScriptObject object. ,Compile方法将正常工作,它将返回一个ScriptObject对象。
But then, there is another error - you are using ToString
before you insert the value into the resulting Dictionary. 但是,还有另一个错误 - 在将值插入到生成的Dictionary中之前使用
ToString
。
Try this out to get started in a better direction: 试试这个以开始更好的方向:
Change the Compile method into returning ScriptObject
, like this: 将Compile方法更改为返回
ScriptObject
,如下所示:
static ScriptObject Compile(string JSource)
{
return (ScriptObject)Microsoft.JScript.Eval.JScriptEvaluate(JSource, Microsoft.JScript.Vsa.VsaEngine.CreateEngine());
}
Then try this: 然后尝试这个:
var x = Compile("var x = { foo: 'baa', bar: { 'nisse': 'kalle' } };");
var foo = x["foo"];
var bar = (ScriptObject)x["bar"];
var nisse = bar["nisse"];
Seems like there are a number of assumptions here. 似乎这里有许多假设。 For instance, that you are getting all the values back on one line.
例如,您将所有值都返回到一行。 You might try setting vvar this way:
您可以尝试以这种方式设置vvar:
string vvar = variables[i].Trim();
This will trim away unwanted newlines and other whitespace. 这将删除不需要的换行符和其他空格。
Can you post the output of compiler.ToString()
for your second example? 你可以为你的第二个例子发布
compiler.ToString()
的输出吗? I would guess that the problem lies in the parsing, not in the JScript compiler. 我猜测问题在于解析,而不是在JScript编译器中。
Also, what happens in cases like this: 此外,在这种情况下会发生什么:
var foo = 'a'; foo = 'b'; foo = 'c';
What value does foo
end up with? foo
最终会带来什么价值?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.