C# Running JavaScript with MsieJavaScript Engine Engine Engine Engine

Keywords: PHP JSON IE encoding

A js script engine code generator is implemented with this tool, which has little information in the research.

I will explain it according to my own application. If there are any mistakes, please point out the discrepancies.

 

Using Nuget to install MsieJavaScript Engine

I used version 3.0.3.0.

 

Add using

1 using MsieJavaScriptEngine;

 

Core code

Use to ensure the automatic release of the engine, and pass in the configuration UseJson2Library = true using the methods of json2.js.

For this json2, chrome and other modern browsers, there are JSON global variables. This variable has two methods, Stringify (converting Js objects into JSON strings) and Parse (converting JSON strings into Js objects).

These two methods are very useful in the transformation of Js objects and Json strings. But IE and other old browsers do not have this global variable. The purpose of json2.js is to determine whether there is a global variable JSON and create it if not.

This engine uses the highest version of IE kernel to parse Js.

 

In my opinion, the SetVariableValue method can directly set object parameters, after all, the Js object is still very good. But I didn't expect to make a mistake if I didn't support it.

So the curve saves the nation and realizes the deserialization of json strings to Js objects

 1             using (var jsEngine = new MsieJsEngine(new JsEngineSettings() { UseJson2Library = true }))
 2             {
 3                 jsEngine.SetVariableValue("result", "");
 4                 //Set up js The variable is json String value
 5                 jsEngine.SetVariableValue("model", Json.ToJson(new EngineData(_table, _cols)));
 6                 //implement JSON.parse()Converts a string to js object
 7                 jsEngine.Execute("model = JSON.parse(model);");
 8 
 9                 ResultTextBox.Text = jsEngine.Evaluate<string>(tmpl);
10             }

 

Attach all Api s

Names and parameter names are almost intuitive and unannotated translations have been busy recently.

Look at the method name as if EmbedHostObject is a method that can also set object parameters, and then I tried it, but it didn't work. Meditation...

        public object CallFunction(string functionName, params object[] args);

        public T CallFunction<T>(string functionName, params object[] args);

        public void CollectGarbage();
        
        public void Dispose();

        public void EmbedHostObject(string itemName, object value);

        public void EmbedHostType(string itemName, Type type);

        public object Evaluate(string expression);

        public object Evaluate(string expression, string documentName);

        public T Evaluate<T>(string expression);

        public T Evaluate<T>(string expression, string documentName);

        public void Execute(string code);

        public void Execute(PrecompiledScript precompiledScript);

        public void Execute(string code, string documentName);

        public void ExecuteResource(string resourceName, Type type);

        public void ExecuteResource(string resourceName, Assembly assembly);

        public object GetVariableValue(string variableName);

        public T GetVariableValue<T>(string variableName);

        public bool HasVariable(string variableName);
        
        public void Interrupt();

        public PrecompiledScript Precompile(string code);

        public PrecompiledScript Precompile(string code, string documentName);

        public PrecompiledScript PrecompileFile(string path, Encoding encoding = null);

        public PrecompiledScript PrecompileResource(string resourceName, Assembly assembly);

        public PrecompiledScript PrecompileResource(string resourceName, Type type);

        public void RemoveVariable(string variableName);
        
        public void SetVariableValue(string variableName, object value);

Posted by businessman332211 on Tue, 23 Jul 2019 23:41:37 -0700