The example in this paper describes how C # performs JS dynamic compilation. Share to everyone for your reference. The implementation method is as follows:
The code is as follows:
Using System;
Using System.CodeDom.Compiler;
Using System.Collections.Generic;
Using System.Linq;
Using System.Reflection;
Using System.Text;
Using System.Threading.Tasks;
Namespace Webpro
{
public class Jscripta
{
private static readonly CodeDomProvider _provider = new Microsoft.JScript.JScriptCodeProvider ();
private static Type _evaluatetype;
Private Const string SCRIPTSTR = @ "Package FHS
{
public class Myjs
{
public static function Test1 (PARAMR1)
{
var retstring = paramr1+ ' is invincible! ';
return retstring;
}
}
}”;
public static Object Jscriptrun (String jsmethodname,object[] testparams)
{
Parameters for compilation
CompilerParameters parameters = new CompilerParameters ();
Parameters. GenerateInMemory = true;
CompilerResults results = _provider.compileassemblyfromsource (parameters, SCRIPTSTR);
Assembly Assembly = results.compiledassembly;
Content in a dynamically compiled script
_evaluatetype = assembly. GetType ("FHS. Myjs ");
Executes the specified method and passes the arguments
Object retobj = _evaluatetype.invokemember (Jsmethodname, BindingFlags.InvokeMethod,
NULL, NULL, TESTPARAMS);
return retobj;
}
}
}
I hope this article is helpful to everyone's C # programming.
In addition to the Declaration,
Running GuestArticles are original, reproduced please link to the form of the address of this article
C # The method of executing JS dynamic compilation
This address: http://www.paobuke.com/develop/c-develop/pbk23186.html
Related content How to reverse output string in C # MD5 simple validation of C # with Java (instance code) TORTOISESVN using tutorial C # to traverse a folder and upload all files in a folder after error case analysis
C # implementations export data to Word or Excel methods in C # the tail recursion and the continuation of C # image brightness adjustment The high Concurrency database Control access code written in C #
C # The method of executing JS dynamic compilation