string a = "int.parse(\"3+\")" ;
string b = "+" ;
string c = "4" ;
string d = a + b + c ;我把变量的d的值当成代码来运行,结果为7.怎么来实现啊,如同int.Parse("3")+4这个代码的效果
string b = "+" ;
string c = "4" ;
string d = a + b + c ;我把变量的d的值当成代码来运行,结果为7.怎么来实现啊,如同int.Parse("3")+4这个代码的效果
解决方案 »
- select * from sysobjects where name = N'Car' and xtype= N'U',‘Car’前面的N有什么作用?
- C#控制热敏票据打印机(USB接口)
- webbrowser一加载flash站程序就关了,怎么回事啊
- 请教类似 动易 {PE.Label id="内容信息列表" bindStyle="表格式" /}解析 正则
- 如何定位DataRow在Dataview中的索引位置?
- C#学习群 13822746,真诚欢迎爱好c#的朋友加入!
- 请问如何在C#中创建SQLXMLBulkLoad 对象?
- C# 如何调用Mstsc.exe进行远程桌面连接
- combobox怎么手工设置 item和对应的值 ?
- 一个简单的问题
- 急问:.net中如何实现插入C#语句
- 遍历从N个元素中取M个元素可能组合的最佳算法
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>调用命令行编译器</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<FONT face="宋体">
<asp:TextBox id="TextBox1" runat="server" Width="448px"></asp:TextBox>
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
<DIV ms_positioning="FlowLayout"><b>输入表达式,可输出结果。</b>
<br>
例如输入“100 + 41*0.5”,那么可以计算出结果120.5并输出。</DIV>
</FONT>
</form>
</body>
</HTML>
=========================================
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;namespace bsTest2005_8_16.Samples
{
/// <summary>
/// 调用命令行编译器 的摘要说明。
/// </summary>
public class 调用命令行编译器 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Button Button1;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
} private object GetJedgement(string expression)
{
Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider(); System.CodeDom.Compiler.ICodeCompiler comp = provider.CreateCompiler(); System.CodeDom.Compiler.CompilerParameters cp = new System.CodeDom.Compiler.CompilerParameters(); cp.ReferencedAssemblies.Add("system.dll") ;
cp.ReferencedAssemblies.Add("system.data.dll") ;
cp.ReferencedAssemblies.Add("system.xml.dll") ;
cp.GenerateExecutable = false ;
cp.GenerateInMemory = true ; string code = @"using System;
using System.Data;
using System.Xml;
public class Judgement
{
public object GetJude()
{
return (" + expression + @");
}
}" ;
System.CodeDom.Compiler.CompilerResults cr = comp.CompileAssemblyFromSource(cp,code);
System.Diagnostics.Debug.Write(code); if(cr.Errors.HasErrors)
{
System.Text.StringBuilder errorMsg = new System.Text.StringBuilder();
foreach(System.CodeDom.Compiler.CompilerError err in cr.Errors)
{
errorMsg.Append(err.ErrorText );
}
System.Diagnostics.Debug.WriteLine(errorMsg.ToString());
throw new System.Exception("编译错误: " + errorMsg.ToString());
//return false;
}
else
{
System.Reflection.Assembly tmp = cr.CompiledAssembly;
object _Compiled = tmp.CreateInstance("Judgement");
System.Reflection.MethodInfo mi = _Compiled.GetType().GetMethod("GetJude");
return mi.Invoke(_Compiled,null);
}
}
private void Button1_Click(object sender, System.EventArgs e)
{
string tmp = this.TextBox1.Text;
Response.Write(this.GetJedgement(tmp).ToString());
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion
}
}
System.Data.OleDb.OleDbConnection adoConn=new System.Data.OleDb.OleDbConnection("connectionString随便连一个数据库就可以了");
adoConn.Open();
System.Data.OleDb.OleDbCommand cmd=adoData.adoConn.CreateCommand();
string s1="3";
cmd.CommandText="select "+s1+"+4";//变量计算表达式
string ss=cmd.ExecuteScalar().ToString();//返回结果
Console.WriteLine(ss);cmd.Dispose();
adoConn.Close();
adoConn.Dispose();
//哈哈,利用SQL select 功能来计算,简单吧,牛吧,接分!!^-^。