In the past, when I was working on a workflow project, one of the items was that when the user made a process definition, he could write a script to control the jump of the activity. These scripts are defined and stored in the database, when a process is started, the workflow engine controls the execution sequence of the activity. String-type two activities are relatively simple, but some of the activities reach the next activity with conditional judgment, or there are multiple branches, it's easy. You only need to add a field to the database table to implement it. To be more complex, you need to implement it through scripts. I didn't have enough experience at the time, but I didn't find a quick solution for a few days. I was not sure about writing a Custom Script Engine myself, and I didn't have enough time. I 'd like to find it on the Internet. It took some time, I found a solution that I think is better, and wrote it to share it with you.
The following two sections describe the implementation and application.
1. Use MSScriptControl
Download Windows Script Control from Microsoft website. It is an ActiveX (R) Control, so I used Interop in. NET. After the download and installation are complete, create a C # Windows application project, select the reference node in solution Resource Manager, right-click and choose add reference from the shortcut menu, and the Add reference dialog box is displayed, click Browse to find the directory for installing Windows Script Control and select msscript. ocx file. A MSScriptControl component is added to the reference node. The following are all objects after Interop.
ScriptControl provides simple interfaces for the host Script engine that supports ActiveX (TM) scripts. Next, we will explain the attributes and methods of the ScriptControl converted to the ScriptControlClass class.
Attribute
AllowUI attribute: applies to the user interface elements displayed by ScriptControl itself or Scirpt engine, and can be read and written.
CodeObject attribute: return object, which is used to call public members of a specified module. Read-only.
Error attribute: return the Error object, which contains detailed information about the last Error. Read-only.
Language attribute: sets or returns the name of the Script Language in use. Read/write.
Modules attribute: a set of return Modules for the ScriptControl object. Read-only.
Procedures attribute: return the process set defined in the specified module. Read-only.
SitehWnd property: set or return the hWnd of the window. By executing Script code, this window is used to display the dialog box and other user interface elements. Read/write.
State attribute: sets or returns the mode of the ScriptControl object. Read/write.
Timeout attribute: set or return time (MS). After this time, you can choose to abort the execution of the Script code or allow the code to continue. Read/write.
UseSafeSubset attribute: sets or returns a Boolean value to indicate whether the host application has confidentiality requirements. If the host application requires security control, UseSafeSubset is True; otherwise, it is False. Read/write.
Method
AddCode: add the specified code to the module. You can call the AddCode method multiple times.
AddObject method: Make the Host Object Model Available to the Script engine.
Eval method: calculates the expression and returns the result.
ExecuteStatement method: run the specified statement.
Reset method: discard all Script code and objects that have been added to ScriptControl.
Run method: Run the specified process.
Event
Error Event: This event occurs when a running Error occurs.
Timeout event: This event occurs when the time specified by the Timeout attribute is exceeded and the End is selected in the result dialog box.
Additional points
If the AllowUI attribute is set to false, statements such as the displayed dialog box do not work, such as MsgBox statements in VBScript and alert in javascript. If the executed scripts exceed the milliseconds set in TimeOut, the dialog box that exceeds the time reminder will not pop up, and vice versa. Resetting the Language attribute will clear the code loaded by AddCode. For the TimeOut attribute, when the time-out occurs, ScriptControl checks the AllowUI attribute of the object, determine whether to allow display of user interface elements.
For more information, see the MSDN documentation.
To make the control easier to use, I wrapped it in a ScriptEngine class. The following is the complete code:
Using System;
Using MSScriptControl;
Using System. Text;
Namespace ZZ
{
/// <Summary>
/// Script Type
/// </Summary>
Public enum ScriptLanguage
{
/// <Summary>
/// JScript Language
/// </Summary>
JScript,
/// <Summary>
/// VBscript script language
/// </Summary>
VBscript,
/// <Summary>
/// Javascript script language
/// </Summary>
Javascript
}
/// <Summary>
/// Script running error proxy
/// </Summary>
Public delegate void RunErrorHandler ();
/// <Summary>
/// Script running timeout proxy
/// </Summary>
Public delegate void RunTimeoutHandler ();
/// <Summary>
/// ScriptEngine class
/// </Summary>
Public class ScriptEngine
{
Private ScriptControl msc;
File: // setScript running error event
Public event RunErrorHandler RunError;
File: // setTask execution timeout
Public event RunTimeoutHandler RunTimeout;
/// <Summary>
/// Constructor
/// </Summary>
Public ScriptEngine (): this (ScriptLanguage. VBscript)
{
}
/// <Summary>
/// Constructor
/// </Summary>
/// <Param name = "language"> Script Type </param>
Public ScriptEngine (ScriptLanguage language)
{
This. msc = new ScriptControlClass ();
This. msc. UseSafeSubset = true;
This. msc. Language = language. ToString ();
(DScriptControlSource_Event) this. msc). Error + = new DScriptControlSource_ErrorEventHandler (ScriptEngine_Error );
(DScriptControlSource_Event) this. msc). Timeout + = new DScriptControlSource_TimeoutEventHandler (ScriptEngine_Timeout );
}
/// <Summary>
/// Run the Eval Method
& Nbs