using System;
using System.CodeDom.Compiler;
using System.Reflection;
using Microsoft.JScript;namespace OdeToCode.Utility
{
public class Evaluator
{
public static int EvalToInteger(string statement)
{
string s = EvalToString(statement);
return int.Parse(s.ToString());
} public static double EvalToDouble(string statement)
{
string s = EvalToString(statement);
return double.Parse(s);
} public static string EvalToString(string statement)
{
object o = EvalToObject(statement);
return o.ToString();
} public static object EvalToObject(string statement)
{
return _evaluatorType.InvokeMember(
"Eval",
BindingFlags.InvokeMethod,
null,
_evaluator,
new object[] { statement }
);
}
static Evaluator()
{
ICodeCompiler compiler;
compiler = new JScriptCodeProvider().CreateCompiler(); CompilerParameters parameters;
parameters = new CompilerParameters();
parameters.GenerateInMemory = true;
CompilerResults results;
results = compiler.CompileAssemblyFromSource(parameters, _jscriptSource); Assembly assembly = results.CompiledAssembly;
_evaluatorType = assembly.GetType("Evaluator.Evaluator");
_evaluator = Activator.CreateInstance(_evaluatorType);
}
private static object _evaluator = null;
private static Type _evaluatorType = null;
private static readonly string _jscriptSource =
@"package Evaluator
{
class Evaluator
{
public function Eval(expr : String) : String
{
return eval(expr);
}
}
}";
}
}
调用:MessageBox.Show(Evaluator.EvalToString("!(0&&1)||1")); //结果为trueMessageBox.Show(Evaluator.EvalToInteger("3+2*4").ToString()); //结果为11
using System.CodeDom.Compiler;
using System.Reflection;
using Microsoft.JScript;namespace OdeToCode.Utility
{
public class Evaluator
{
public static int EvalToInteger(string statement)
{
string s = EvalToString(statement);
return int.Parse(s.ToString());
} public static double EvalToDouble(string statement)
{
string s = EvalToString(statement);
return double.Parse(s);
} public static string EvalToString(string statement)
{
object o = EvalToObject(statement);
return o.ToString();
} public static object EvalToObject(string statement)
{
return _evaluatorType.InvokeMember(
"Eval",
BindingFlags.InvokeMethod,
null,
_evaluator,
new object[] { statement }
);
}
static Evaluator()
{
ICodeCompiler compiler;
compiler = new JScriptCodeProvider().CreateCompiler(); CompilerParameters parameters;
parameters = new CompilerParameters();
parameters.GenerateInMemory = true;
CompilerResults results;
results = compiler.CompileAssemblyFromSource(parameters, _jscriptSource); Assembly assembly = results.CompiledAssembly;
_evaluatorType = assembly.GetType("Evaluator.Evaluator");
_evaluator = Activator.CreateInstance(_evaluatorType);
}
private static object _evaluator = null;
private static Type _evaluatorType = null;
private static readonly string _jscriptSource =
@"package Evaluator
{
class Evaluator
{
public function Eval(expr : String) : String
{
return eval(expr);
}
}
}";
}
}
调用:MessageBox.Show(Evaluator.EvalToString("!(0&&1)||1")); //结果为trueMessageBox.Show(Evaluator.EvalToInteger("3+2*4").ToString()); //结果为11
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货