Adjustment of scripts such as VBScript, JavaScript in C #
Author: Zheng Zuo
2004-04-26
In the case of a workflow project, there is an item to write the script to control the jump of the active activity when the user develops the process definition, and these scripts define in the database, when the process is started, the workflow engine Controlling the active execution order, the two activities of the string are relatively simple, but some activities have conditional judgments to the next event, or there are multiple branches, simple, as long as you add a field in the database table, you can implement, Complicated, you need to be implemented through scripts. At that time, the experience did not find a quick solution in a few days, I want to write a custom script engine is not grasped, and the time is not enough, still find it online, spend some time, still find a self-thinking comparison A good solution, write it out with you. The implementation and application will be described below by two parts. One. Download Windows Script Control on Microsoft's website using MSScriptControl, it is an ActiveX (R) control, so I use my interpo in .NET. After downloading the installation, create a new C # Windows application project, select the reference node in the Solution Explorer, right-click the Select Add Reference menu, pop up Add Reference dialog box, click Browse to find the directory where Windows Script Control is installed, select The MSScript.ocx file is determined. Then add a MSScriptControl component under the reference node. Here is all objects behind his intero.
ScriptControl provides a simple interface for host Script Engines that support ActiveX (TM) Script. Next we describe some instructions for the properties and methods of the ScriptControl Class ScriptControlClass class.
Attributes
Allowui attribute: Applying to the user interface element displayed by the ScriptControl itself or the Scirpt engine, readable.
CodeObject Property: Returns an object, which is used to call the public member of the specified module. Read only.
Error property: Returns an Error object, which contains the relevant details of the last error that occur. Read only.
Language Properties: Set or returns the Script language name being used. Ready to be read.
Modules properties: Returns the module collection for the ScriptControl object. Read only.
Procedures Property: Returns the process collection defined in the specified module. Read only.
SitehWnd property: Set or return to the window's HWnd, this window is used to display dialogs and other user interface elements by performing Script code. Ready to be read.
State Properties: Set or returns the mode of the ScriptControl object. Ready to be read.
TimeOUt property: Set or return time (milliseconds), this time the user can select the execution of the Script code or allow the code to continue. Ready to be read.
UsesaFesubset property: Set or return to the Boolean value, indicating whether the host application has confidentiality requirements. If the host application needs to be safely controlled, UseSafesubset is true, otherwise false. Ready to be read.
method
AddCode method: Add a specified code to the module. The AddCode method can be called multiple times.
AddObject method: Make the host object model to the Script engine.
Eval method: calculate the expression and return the result.
ExecuteStatement method: Perform the specified statement. Reset method: Abandon all Script code and objects that have been added to ScriptControl.
RUN method: Run the specified process.
event
Error event: This event occurs when an error occurs.
Timeout event: This event occurs when the timeout attribute is specified and the user selects END in the Results dialog box.
Additional points
Allowui Properties If set to false, the statement like the dialog box does not work, such as the msgbox statement in VBScript, the Alert in JavaScript, etc., and if the executed script exceeds the number of milliseuits set up to Timeout, it will not jump out of time Remind the dialog box, reverse it; reset the Language property clearing the code loaded by addCode; for the timeout property, the ScriptControl checks the object's allowui property, determines whether the user interface element is allowed.
If the reader needs more detailed understanding, you can view the MSDN document.
In order to make the control more easily, I wrapped in a scriptengine class, below is the full code:
Using system; using msscriptControl; usings zz {///
((DScriptControlSource_Event) this.msc) .Timeout = new DScriptControlSource_TimeoutEventHandler (ScriptEngine_Timeout);} ///
RunfunctionName, Ref parameters;} ///
} Set {this.msc.allowui = value;}} ///
Long test program code, listed below the main part: using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace ZZ {public class Form1: System.Windows.Forms.Form {private ScriptEngine scriptEngine; private System.Windows.Forms.CheckBox checkBoxAllowUI; private System.Windows.Forms.TextBox textBoxResult; private System.Windows.Forms.NumericUpDown numericUpDownTimeout; private System.Windows.Forms.TextBox textBoxCodeBody; private System.Windows.Forms.Button buttonRun; private System.Windows.Forms.Button buttonCancel; private System.Windows.Forms.ComboBox comboBoxScript; private System.Windows.Forms.TextBox textBoxParams; private System.Windows.Forms.RadioButton Radiobuttoneval; private system.windows.Forms.Radiobutton radiobuttonrun; private system.windows.Forms.TextBox textBoxMethodName; private system. ComponentModel.Container components = null; public Form1 () {InitializeComponent (); this.comboBoxScript.SelectedIndex = 0; this.scriptEngine = new ScriptEngine (); this.scriptEngine.UseSafeSubset = true; this.scriptEngine.RunError = new RunErrorHandler (scriptEngine_RunError); this.scriptEngine.RunTimeout = new RunTimeoutHandler (scriptEngine_RunTimeout);} protected override void Dispose (bool disposing) {if (disposing) if (! components = null) components.Dispose ();
Base.dispose (Disposing);} #Region Windows Form Designer Generated Code Private Void InitializationComponent () {// 省 略} #ENDREGON [Stathread] static void main () {Application.run (New Form1 ());} // run script private void buttonRun_Click (object sender, System.EventArgs e) {this.scriptEngine.Reset (); this.scriptEngine.Language = (ScriptLanguage) Enum.Parse (typeof (ScriptLanguage), this.comboBoxScript.SelectedItem.ToString ()); this.scriptEngine.Timeout = (int) this.numericUpDownTimeout.Value; this.scriptEngine.AllowUI = this.checkBoxAllowUI.Checked; if (this.radioButtonEval.Checked) // {this.textBoxResult.Text method performed Eval = this.ScripTENGINE.EVAL (THISTEXTBOXMETHODNAME.TEXT "(" this.textBoxParams.Text ")" ")" ")" ")", this.textboxcodebody.text) .tostring ();} el SE // Execute RUN Method {String [] parameters = (String []) this.textBoxparams.Text.Split (','); object [] paramarray = new object [parameters.length]; for (int i = 0; i } // error function private void scriptEngine_RunError () {MessageBox.Show ( "RunError execute script error!");} Private void scriptEngine_RunTimeout () {MessageBox.Show ( "RunTimeout execute the script timeout, raise an error!");}}} Write a JavaScript function in the text box. Enter 12, output 12000012. If the timeout time is adjusted to 1 millisecond, then the script will jump out of the overtime reminder box, while stimulating the event. Summary, the JavaScript script is demonstrated above. If you have a reader to write some VBSript functions, the scripting language is only listed, and he also supports other scripts, if you need to add. Also, since COM is called, some return values are OBEJCT types and need to be converted. In the 9CBS technology forum C # plate, there is a friend asking this question. For friends who encounter such problems, I hope to get some help you need through this article, I am very happy to communicate with .NET friends. , My email address zhzuocn@163.com.