C # The method of executing JS dynamic compilation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.