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 js.net (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 }
5
6 }
7
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 js.net 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"