For a long time, the JS engine, msscript. ocx, InterOP. msscriptcontrol. dll, has been used to calculate string expressions.
However, during the deployment on Monday, we found that the 64-bit unsupported problem exists. Although it was solved after 32-bit is enabled, it was always a problem.
Yesterday, using the C # code compilation function, we finally solved the problem of C # string expression calculation. Both methods and code come from the Internet. Thank you very much. I also shared my modified code with you.
I. Script Method
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
}
}
Method 2 C #
Using system;
Using system. Collections. Generic;
Using system. text;
Using system. codedom;
Using Microsoft. CSHARP;
Using system. codedom. compiler;
Using system. reflection;
Namespace myquery. csharpscript
{
/// <Summary>
/// C # string expression computing Engine
/// By Jia Shiyi
/// </Summary>
Public class csharpengine
{
/// <Summary>
/// C # note the case sensitivity for expression Calculation
/// </Summary>
/// <Param name = "expression"> common C # string expression </param>
/// <Returns> </returns>
Public object eval (string expression)
{
String classname = "calc ";
String methodname = "run ";
// Create a compiler instance.
Csharpcodeprovider complier = new csharpcodeprovider ();
// Set the compilation parameters.
Compilerparameters CP = new compilerparameters ();
CP. referencedassemblies. Add ("system. dll ");
CP. referencedassemblies. Add ("system. Data. dll ");
CP. referencedassemblies. Add ("system. xml. dll ");
CP. generateexecutable = false;
CP. generateinmemory = true;
// Create dynamic code.
Stringbuilder code = new stringbuilder ();
Code. append ("using system; \ n ");
Code. append ("using system. Text; \ n ");
Code. append ("using system. Text. regularexpressions; \ n ");
Code. append ("using system. collections; \ n ");
Code. append ("using system. Collections. Generic; \ n ");
Code. append ("using system. Collections. Specialized; \ n ");
Code. append ("using system. Data; \ n ");
Code. append ("using system. xml; \ n ");
Code. append ("public class" + classname + "\ n ");
Code. append ("{\ n ");
Code. append ("public object" + methodname + "() \ n ");
Code. append ("{\ n ");
Code. append ("return" + expression + "; \ n ");
Code. append ("} \ n ");
Code. append ("}");
// Compile the code.
Compilerresults Cr = complier. compileassemblyfromsource (CP, code. tostring ());
If (Cr. errors. haserrors)
{
Stringbuilder error = new stringbuilder ();
Error. append ("error compiling expression :");
Foreach (compilererror err in Cr. Errors)
{
Error. appendformat ("{0} \ n", Err. errortext );
}
Throw new exception ("error compiling expression:" + error. tostring ());
}
Else
{
// Obtain the compiled assembly.
Assembly = Cr. compiledassembly;
// Call the method dynamically.
Object EVAL = assembly. createinstance (classname );
Methodinfo method = eval. GetType (). getmethod (methodname );
Return method. Invoke (Eval, null );
}
}
}
}
A configuration-Based C # rapid development architecture can be applied to the development of information management, workflows, OA, portal websites, and other systems.
Demo address: http: // 121.18.78.216/