C#中如何将输入的字符串识别为(转化为)程序语句 我输入了一组有意义字符串(比如数学公式),如何将输入的字符串识别为(转化为)程序语句运行呢? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 Javascript中的eval("a+b")在C#应该是没有办法解释的.也就是说有意义的字符串转换成程序语句应该是不成的,不知道高手们有没有办法 通过CODEDOM+反射来处理,不过你的语句一定要正确.CODEDOM可以临时生成代码及编译成DLL,反射可以调用生成的DLL 请参考这篇文章:动态地生成用户输入的函数表达式(C#) http://www.cnblogs.com/skyivben/archive/2005/10/31/265861.html// Expression.cs - 动态生成数学表达式并计算其值 // 表达式使用 C# 语法,可带一个的自变量(x)。 // 表达式的自变量和值均为(double)类型。 // 使用举例: // Expression expression = new Expression("Math.Sin(x)"); // Console.WriteLine(expression.Compute(Math.PI / 2)); // expression = new Expression("double u = Math.PI - x;" + // "double pi2 = Math.PI * Math.PI;" + // "return 3 * x * x + Math.Log(u * u) / pi2 / pi2 + 1;"); // Console.WriteLine(expression.Compute(0)); using System; using System.CodeDom.Compiler; using Microsoft.CSharp; using System.Reflection; using System.Text; namespace Skyiv.Util { sealed class Expression { object instance; MethodInfo method; public Expression(string expression) { if (expression.IndexOf("return") < 0) expression = "return " + expression + ";"; string className = "Expression"; string methodName = "Compute"; CompilerParameters p = new CompilerParameters(); p.GenerateInMemory = true; CompilerResults cr = new CSharpCodeProvider().CompileAssemblyFromSource(p, string. Format("using System;sealed class {0}{{public double {1}(double x){{{2}}}}}", className, methodName, expression)); if(cr.Errors.Count > 0) { string msg = "Expression(\"" + expression + "\"): \n"; foreach (CompilerError err in cr.Errors) msg += err.ToString() + "\n"; throw new Exception(msg); } instance = cr.CompiledAssembly.CreateInstance(className); method = instance.GetType().GetMethod(methodName); } public double Compute(double x) { return (double)method.Invoke(instance, new object [] { x }); } } } // ExpressionTest.cs - 动态生成数学表达式并计算其值的测试程序 // 编译方法: csc ExpressionTest.cs Expression.cs using System; using Skyiv.Util; namespace Skyiv.Test { class ExpressionTest { static void Main(string [] args) { try { if (args.Length > 0) { Console.WriteLine("f(x): {0}", args[0]); Expression expression = new Expression(args[0]); for (int i = 1; i < args.Length; i++) { double x = double.Parse(args[i]); Console.WriteLine("f({0}) = {1}", x, expression.Compute(x)); } } else Console.WriteLine("Usage: ExpressionTest expression [ parameters ]"); } catch (Exception ex) { Console.WriteLine("错误: " + ex.Message); } } } } 如果只是解析数学公式的话方法也不少,差不多5到6种1.按逆波兰表达式自己解析2.使用第三方语言引擎ironpython ,jscript,javascript,vbscript3.使用codedom4.反射5.借用dataTable运算6.使用第三方科学计算工具如:matlab,labview,excel artdialog的确定取消对话框如何使用,传递返回值? 一个关闭模式窗体 用 ADO.net 开发了一个软件 怎么实现打开软件之后 有语音提示 例如提示:欢迎使用 如何判断textbox里是否已经全选 急求:如何为Dcom动态配置账户 子窗体如何向父窗体传值? 数据库查询问题 超过50M的大图片缩小时提示 内存不足 帮帮忙!不难的问题 怎么移动dataGrid1记录? 客户端接受图片数接受后,byte[] 的大小如何设定,已经如何把接受的byte[]组合成一个byte[]?? c#小问题
也就是说有意义的字符串转换成程序语句应该是不成的,不知道高手们有没有办法
CODEDOM可以临时生成代码及编译成DLL,反射可以调用生成的DLL
动态地生成用户输入的函数表达式(C#)
http://www.cnblogs.com/skyivben/archive/2005/10/31/265861.html// Expression.cs - 动态生成数学表达式并计算其值
// 表达式使用 C# 语法,可带一个的自变量(x)。
// 表达式的自变量和值均为(double)类型。
// 使用举例:
// Expression expression = new Expression("Math.Sin(x)");
// Console.WriteLine(expression.Compute(Math.PI / 2));
// expression = new Expression("double u = Math.PI - x;" +
// "double pi2 = Math.PI * Math.PI;" +
// "return 3 * x * x + Math.Log(u * u) / pi2 / pi2 + 1;");
// Console.WriteLine(expression.Compute(0));
using System;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
using System.Reflection;
using System.Text;
namespace Skyiv.Util
{
sealed class Expression
{
object instance;
MethodInfo method;
public Expression(string expression)
{
if (expression.IndexOf("return") < 0) expression = "return " + expression + ";";
string className = "Expression";
string methodName = "Compute";
CompilerParameters p = new CompilerParameters();
p.GenerateInMemory = true;
CompilerResults cr = new CSharpCodeProvider().CompileAssemblyFromSource(p, string.
Format("using System;sealed class {0}{{public double {1}(double x){{{2}}}}}",
className, methodName, expression));
if(cr.Errors.Count > 0)
{
string msg = "Expression(\"" + expression + "\"): \n";
foreach (CompilerError err in cr.Errors) msg += err.ToString() + "\n";
throw new Exception(msg);
}
instance = cr.CompiledAssembly.CreateInstance(className);
method = instance.GetType().GetMethod(methodName);
}
public double Compute(double x)
{
return (double)method.Invoke(instance, new object [] { x });
}
}
}
// ExpressionTest.cs - 动态生成数学表达式并计算其值的测试程序
// 编译方法: csc ExpressionTest.cs Expression.cs
using System;
using Skyiv.Util;
namespace Skyiv.Test
{
class ExpressionTest
{
static void Main(string [] args)
{
try
{
if (args.Length > 0)
{
Console.WriteLine("f(x): {0}", args[0]);
Expression expression = new Expression(args[0]);
for (int i = 1; i < args.Length; i++)
{
double x = double.Parse(args[i]);
Console.WriteLine("f({0}) = {1}", x, expression.Compute(x));
}
}
else Console.WriteLine("Usage: ExpressionTest expression [ parameters ]");
}
catch (Exception ex)
{
Console.WriteLine("错误: " + ex.Message);
}
}
}
}
2.使用第三方语言引擎ironpython ,jscript,javascript,vbscript
3.使用codedom
4.反射
5.借用dataTable运算
6.使用第三方科学计算工具如:matlab,labview,excel