English 中文(简体)
如何使用C#和在阵列中恢复价值?
原标题: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). What s the best way to do this? I m using the Microsoft.Jscript class and your methods for it work.

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*vars*", 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());
        }

在某些案件中,这种做法是罚款的:

var jscript = "var x =  foo ; var y =  baa ;"; 
var output = ParseVariables(jscript); 
var val = output["y"]; //baa 
var val2 = output["x"]; //foo

但情况如下:

var jscript = "var x = {}; x[ foo ] =  baa ; x[ baa ] =  test ;"; 
var output = ParseVariables(jscript); 
var val = output["x[ foo ]"]; //not works 

我如何这样做? 任何帮助都值得赞赏。 感谢!

最佳回答

由于你的做法是将J张本源代码分为按半科分立的楚克(;),只有var之间的部分;将使用你的汇编方法加以汇编。

如果你将你的焦源代码改为var x ={“foo”:“baa”、“baa”:“test”};,则该混合物法将适当运作,并将归还标本。

But then, there is another error - you are using ToString before you insert the value into the resulting Dictionary.

Try this out to get started in a better direction:

Change the Compile method into returning ScriptObject, like this:

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"];
问题回答

这里有一些假设。 例如,你正在把所有价值观重新置于一线。 您可以尝试以这种方式:

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? I would guess that the problem lies in the parsing, not in the JScript compiler.

此外,在类似情况下发生的情况:

var foo =  a ;
foo =  b ;
foo =  c ;

foo结尾处?





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签