How to Implement the function of dynamically executing code (JS eval) in net?

Source: Internet
Author: User

In the past, JS often used its eval method to complete the dynamic process.Code.
However, there seems to be no good way to replace it in net. Although there seems to be a code compilation class, it is too complicated, and the execution of a simple piece of code is really not worth the candle.
Today, when I talked to my colleagues about a workflow, I heard about its dynamic code node and found the function I have been using for a long time.
Share it with you.

Principle ﹕
Use (because there is an eval method in it) to write a class. Add a new method to the class to execute dynamic JS Code.
Then, use jsc.exe to compile it into a DLL.
Add it to the C # project and pass in the dynamic code to call this method of this category. The result is displayed.
1. Step 1: Add a JS File
Myeval. js


1 Class myeval {
2   Function Execute (Code: string): String { // Method.
3 Return Eval (CODE );
4 }
6 }


2. Compile to DLL

JSC/Target: Library/out: myeval. dll myeval. js


3. Compile the test file
Testeval. CS


Using System;

Class Test {

  Public   Static   Void Main () {
String Code =   " VaR result: Int = 0; Result = 1? \ " Success \ " :\ " Failed \ "" ;
Myeval eval =   New Myeval ();
String Result = Eval.exe cute (CODE );
Console. writeline ( " The result is: "   + Result );



4. Compile to EXE (because class is used, you must add the Microsoft. JScript reference)

CSC/R: Microsoft. jscript. dll/R: myeval. dll test. CS


5.execute test.exe

The result is "failed"




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: 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.