[Csharp]
/// <Summary>
/// Calculate the operators in the string as normal, for example, by four
/// </Summary>
/// <Param name = "expression"> standard expressions, such as 1 + 15*0.5-200 </param>
/// <Returns> return the calculated value, which can be of any valid data type </returns>
Protected static object MathCalculate (string expression)
{
Object retvar = null;
Microsoft. CSharp. CSharpCodeProvider provider = new Microsoft. CSharp. CSharpCodeProvider ();
System. CodeDom. Compiler. CompilerParameters cp = new System. CodeDom. Compiler. CompilerParameters (
New string [] {@ "System. dll "});
StringBuilder builder = new StringBuilder ("using System; class CalcExp {public static object Calc () {return \" expression \";}}");
Builder. Replace ("\" expression \ "", expression );
String code = builder. ToString ();
System. CodeDom. Compiler. CompilerResults results;
Results = provider. CompileAssemblyFromSource (cp, new string [] {code });
If (results. Errors. HasErrors)
{
Retvar = null;
}
Else
{
System. Reflection. Assembly a = results. CompiledAssembly;
Type t = a. GetType ("CalcExp ");
Retvar = t. InvokeMember ("Calc", System. Reflection. BindingFlags. Public | System. Reflection. BindingFlags. Static | System. Reflection. BindingFlags. InvokeMethod
, System. Type. DefaultBinder, null, null );
}
Return retvar;
}