Ars*_*yan 17 .net c# evaluation expression
有许多算法来评估表达式,例如:
有没有办法用C#.net反射或其他现代.net技术评估任何数学表达式?
Luk*_*keH 19
除了Thomas的答案之外,实际上可以直接从C#访问(已弃用的)JScript库,这意味着您可以使用相当于JScript的eval函数.
using Microsoft.JScript; // needs a reference to Microsoft.JScript.dll
using Microsoft.JScript.Vsa; // needs a reference to Microsoft.Vsa.dll
// ...
string expr = "7 + (5 * 4)";
Console.WriteLine(JScriptEval(expr)); // displays 27
// ...
public static double JScriptEval(string expr)
{
// error checking etc removed for brevity
return double.Parse(Eval.JScriptEvaluate(expr, _engine).ToString());
}
private static readonly VsaEngine _engine = VsaEngine.CreateEngine();
Run Code Online (Sandbox Code Playgroud)
Jor*_*ren 13
这当然是可能的.该CodeSnippetCompileUnit类做基本上是这样的.我给你写了一些示例用法代码.您需要包含这些名称空间:
这是代码:
string source = @"
class MyType
{
public static int Evaluate(<!parameters!>)
{
return <!expression!>;
}
}
";
string parameters = "int a, int b, int c";
string expression = "a + b * c";
string finalSource = source.Replace("<!parameters!>", parameters).Replace("<!expression!>", expression);
CodeSnippetCompileUnit compileUnit = new CodeSnippetCompileUnit(finalSource);
CodeDomProvider provider = new CSharpCodeProvider();
CompilerParameters parameters = new CompilerParameters();
CompilerResults results = provider.CompileAssemblyFromDom(parameters, compileUnit);
Type type = results.CompiledAssembly.GetType("MyType");
MethodInfo method = type.GetMethod("Evaluate");
// The first parameter is the instance to invoke the method on. Because our Evaluate method is static, we pass null.
int result = (int)method.Invoke(null, new object[] { 4, -3, 2 });
Run Code Online (Sandbox Code Playgroud)
用任何东西替换'parameters'和'expression',你就得到了一个通用表达式求值器.
如果在results.CompiledAssembly中遇到FileNotFoundException,则代码段无法编译.
您可能还想查看System.CodeDom.CodeSnippetExpression类.它用于更具体地读取表达式,但是表达式本身无法编译,因此您需要使用更多的CodeDom来构建一个工作类和方法.如果您希望能够以编程方式操作正在生成的类,那么这非常有用.CodeSnippetCompileUnit很适合一次生成一个完整的工作类(并且更简单的例子)但是要操作它你必须做不方便的字符串操作.