C # Use JS scripts to calculate the configured text expression

Source: Internet
Author: User

Using system;
Using system. Collections. Generic;
Using system. text;
Using msscriptcontrol;

Namespace myquery. csharpscript
{
/// <Summary>
/// Script running error proxy
/// </Summary>
Public Delegate void runerrorhandler ();

/// <Summary>
/// Script running timeout proxy
/// </Summary>
Public Delegate void runtimeouthandler ();

/// <Summary>
/// Script Processing Engine
/// By open-source Shengshi-source code download network is developed based on network articles and is a product of learning custom scripts.
/// </Summary>
Public sealed class scriptengine
{
Private scriptcontrolclass MSC;
/// <Summary>
/// Define a script running error event
/// </Summary>
Public event runerrorhandler runerror;
/// <Summary>
/// Define the script running timeout event
/// </Summary>
Public event runtimeouthandler runtimeout;

/// <Summary>
/// Constructor
/// </Summary>
Public scriptengine ()
: This (scriptlanguage. Javascript)
{
}

/// <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
/// </Summary>
/// <Param name = "expression"> Expression </param>
/// <Param name = "codebody"> function body </param>
/// <Returns> returned object </returns>
Public object eval (string expression, string codebody)
{
MSC. addcode (codebody );
Return MSC. eval (expression );
}

/// <Summary>
/// Run the eval Method
/// </Summary>
/// <Param name = "language"> script language </param>
/// <Param name = "expression"> Expression </param>
/// <Param name = "codebody"> function body </param>
/// <Returns> returned object </returns>
Public object eval (scriptlanguage language, string expression, string codebody)
{
If (this. Language! = LANGUAGE)
This. Language = language;
Return eval (expression, codebody );
}

/// <Summary>
/// Run the run Method
/// </Summary>
/// <Param name = "mainfunctionname"> name of the entry function </param>
/// <Param name = "Parameters"> parameter </param>
/// <Param name = "codebody"> function body </param>
/// <Returns> returned object </returns>
Public object run (string mainfunctionname, object [] parameters, string codebody)
{
This. MSC. addcode (codebody );
Return MSC. Run (mainfunctionname, parameters );
}

/// <Summary>
/// Run the run Method
/// </Summary>
/// <Param name = "language"> script language </param>
/// <Param name = "mainfunctionname"> name of the entry function </param>
/// <Param name = "Parameters"> parameter </param>
/// <Param name = "codebody"> function body </param>
/// <Returns> returned object </returns>
Public object run (scriptlanguage language, string mainfunctionname, object [] parameters, string codebody)
{
If (this. Language! = LANGUAGE)
This. Language = language;
Return run (mainfunctionname, parameters, codebody );
}

/// <Summary>
/// Discard all script code and objects that have been added to scriptcontrol
/// </Summary>
Public void reset ()
{
This. MSC. Reset ();
}

/// <Summary>
/// Obtain or set the script language
/// </Summary>
Public scriptlanguage Language
{
Get {return (scriptlanguage) enum. parse (typeof (scriptlanguage), this. MSC. Language, false );}
Set {This. MSC. Language = value. tostring ();}
}

/// <Summary>
/// Obtain or set the script execution time, in milliseconds
/// </Summary>
Public int timeout
{
Get {return this. MSC. Timeout ;}
Set {This. MSC. Timeout = value ;}
}

/// <Summary>
/// Set whether to display the user interface elements
/// </Summary>
Public bool allowui
{
Get {return this. MSC. allowui ;}
Set {This. MSC. allowui = value ;}
}

/// <Summary>
/// Whether the host application has confidentiality requirements
/// </Summary>
Public bool usesafesubset
{
Get {return this. MSC. usesafesubset ;}
Set {This. MSC. usesafesubset = true ;}
}

/// <Summary>
/// Triggered by the runerror event
/// </Summary>
Private void onerror ()
{
If (runerror! = NULL)
Runerror ();
}

/// <Summary>
/// Ontimeout event triggered
/// </Summary>
Private void ontimeout ()
{
If (runtimeout! = NULL)
Runtimeout ();
}

Private void scriptengine_error ()
{
Onerror ();
}

Private void scriptengine_timeout ()
{
Ontimeout ();
}
}
/// <Summary>
/// Script language Enumeration
/// </Summary>
Public Enum scriptlanguage
{
/// <Summary>
/// JScript Language
/// </Summary>
JScript,

/// <Summary>
/// VBSCRIPT script language
/// </Summary>
VBScript,

/// <Summary>
/// Javascript script language
/// </Summary>
Javascript
}

}

Welcome to: http: // 121.18.78.216, which is a demonstration platform for ease of query and analysis, workflow, content management, and project management.

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.