怎样将"20*10+5*20"这样的字符串表达式"计算"后返回结果300? 如题? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 using System;using System.CodeDom.Compiler;using Microsoft.CSharp;using System.Reflection;using System.Text;namespace ConsoleApplication22{ /// <summary> /// Class1 的摘要说明。 /// </summary> class Class1 { /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main(string[] args) { // // TODO: 在此处添加代码以启动应用程序 // int i = (int)Calc("20*10+5*20"); Console.WriteLine(i.ToString()); Console.ReadLine(); } public static object Calc(string expression) { string className = "Calc"; string methodName = "Run"; expression=expression.Replace("/","*1.0/"); // 创建编译器实例。 ICodeCompiler complier = (new CSharpCodeProvider().CreateCompiler()); // 设置编译参数。 CompilerParameters paras = new CompilerParameters(); paras.GenerateExecutable = false; paras.GenerateInMemory = true; // 创建动态代码。 StringBuilder classSource = new StringBuilder(); classSource.Append("public class "+ className +"\n"); classSource.Append("{\n"); classSource.Append(" public object " + methodName + "()\n"); classSource.Append(" {\n"); classSource.Append(" return "+ expression + ";\n"); classSource.Append(" }\n"); classSource.Append("}"); //System.Diagnostics.Debug.WriteLine(classSource.ToString()); // 编译代码。 CompilerResults result = complier.CompileAssemblyFromSource(paras, classSource.ToString()); // 获取编译后的程序集。 Assembly assembly = result.CompiledAssembly; // 动态调用方法。 object eval = assembly.CreateInstance(className); MethodInfo method = eval.GetType().GetMethod(methodName); object reobj = method.Invoke(eval, null); GC.Collect(); return reobj; } }} LoveCherry(论成败,人生豪迈;大不了,重头再来!^_^) :=============谢谢,我试试多。 OK,谢谢LoveCherry(论成败,人生豪迈;大不了,重头再来!^_^) 结贴。 int i = (int)Calc("20*10+5*20");保存一下先 求助,Access2007数据库,窗体程序,如何获取datagridview里面表最大的ID(主键自动编号),然后向表中添加数据时,将ID 数据库的设计问题 你们遇到过这种情况吗? 关于 winform 的数据缓存! C#中access数据库 查询 DataGridView 根据 当前鼠标的坐标,得到该坐标的单元格? 关于类型作为泛型传入参数的问题 各位如何对待代码重复? 怎么能将TextBox.Text转换成datetime类型呢? 如何知道一个对象,占用了多少内存呢? 请教高手:关于DataGrid记录的排序问题:上下移动 如何遍历WINFORM中所有的控件??
using System.CodeDom.Compiler;
using Microsoft.CSharp;
using System.Reflection;
using System.Text;namespace ConsoleApplication22
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: 在此处添加代码以启动应用程序
//
int i = (int)Calc("20*10+5*20");
Console.WriteLine(i.ToString());
Console.ReadLine(); } public static object Calc(string expression)
{
string className = "Calc";
string methodName = "Run";
expression=expression.Replace("/","*1.0/");
// 创建编译器实例。
ICodeCompiler complier = (new CSharpCodeProvider().CreateCompiler());
// 设置编译参数。
CompilerParameters paras = new CompilerParameters();
paras.GenerateExecutable = false;
paras.GenerateInMemory = true;
// 创建动态代码。
StringBuilder classSource = new StringBuilder();
classSource.Append("public class "+ className +"\n");
classSource.Append("{\n");
classSource.Append(" public object " + methodName + "()\n");
classSource.Append(" {\n");
classSource.Append(" return "+ expression + ";\n");
classSource.Append(" }\n");
classSource.Append("}");
//System.Diagnostics.Debug.WriteLine(classSource.ToString());
// 编译代码。
CompilerResults result = complier.CompileAssemblyFromSource(paras, classSource.ToString());
// 获取编译后的程序集。
Assembly assembly = result.CompiledAssembly;
// 动态调用方法。
object eval = assembly.CreateInstance(className);
MethodInfo method = eval.GetType().GetMethod(methodName);
object reobj = method.Invoke(eval, null);
GC.Collect();
return reobj; } }
}
=============
谢谢,我试试多。