在asp中,可以用Execute将字符串执行,来计算或者更改变量
dim TestIf :TestIf=False
Execute("If 1=1 Then TestIf = True Else TestIf = False")然后在后面是TestIf的值是True简单说,就是可以执行脚本过程!请问下,在.NET中有没有办法实现这个功能?
dim TestIf :TestIf=False
Execute("If 1=1 Then TestIf = True Else TestIf = False")然后在后面是TestIf的值是True简单说,就是可以执行脚本过程!请问下,在.NET中有没有办法实现这个功能?
解决方案 »
- 求教如何抓去数据,给点思路也行
- Website 不能调试的问题
- SQL數據庫中的數據寫入xml檔的請教
- 一个层次问题讨论?
- 请问,在asp.net中如何提取已绑定在datalist中的数据?请具体说明,谢谢!
- 后台做好了,但是将后台程序移动到前台后,美工已经做好的界面变得面目全飞~~~头痛,大家有没有什么好的办法吗?
- 值的传递问题
- 100分求救---有关DataGrid显示样式的问题.
- 好消息: 有偿兼职开发网站流量统计或进销存系统(C#)! 如果有兴趣跟我联系?
- ===只有10分了,请问怎么把txt或word转换成传真格式(tif或fax),谢谢===
- 哪位大神看看问题啊
- 急急急啊!在应用程序级别之外使用注册为
http://msdn.microsoft.com/zh-cn/library/y2k85ax6.aspx
static string GenerateCode(string str)
{
StringBuilder sb = new StringBuilder();
sb.Append("using System;");
sb.Append(Environment.NewLine);
sb.Append("using System.Collections.Generic;");
sb.Append(Environment.NewLine);
sb.Append("using System.Linq;");
sb.Append(Environment.NewLine);
sb.Append("using System.Web;");
sb.Append(Environment.NewLine);
sb.Append("using System.Web.UI;");
sb.Append(Environment.NewLine);
sb.Append("using System.Text.RegularExpressions;");
sb.Append(Environment.NewLine);
sb.Append("using System.Net;");
sb.Append(Environment.NewLine);
sb.Append("using System.IO;");
sb.Append(Environment.NewLine);
sb.Append("using System.CodeDom.Compiler;");
sb.Append(Environment.NewLine);
sb.Append("using Microsoft.CSharp;");
sb.Append(Environment.NewLine);
sb.Append("using System.Reflection;");
sb.Append(Environment.NewLine);
sb.Append("using System.Text;");
sb.Append(Environment.NewLine);
sb.Append("using System.Data.SqlClient;");
sb.Append(Environment.NewLine);
sb.Append("namespace CMS");
sb.Append(Environment.NewLine);
sb.Append("{");
sb.Append(Environment.NewLine);
sb.Append(" public class DiyCS");
sb.Append(Environment.NewLine);
sb.Append(" {");
sb.Append(Environment.NewLine);
sb.Append(" public DiyCS() {}");
sb.Append(Environment.NewLine);
sb.Append(" public object str;");
sb.Append(Environment.NewLine);
sb.Append(" public object OutPut()");
sb.Append(Environment.NewLine);
sb.Append(" {");
sb.Append(Environment.NewLine);
sb.Append(" str = null;");
sb.Append(Environment.NewLine);
sb.Append(" " + str + "");
sb.Append(Environment.NewLine);
sb.Append(" return str;");
sb.Append(Environment.NewLine);
sb.Append(" }");
sb.Append(Environment.NewLine);
sb.Append(" }");
sb.Append(Environment.NewLine);
sb.Append("}");
return sb.ToString();
} public static string ParseCode(string str)
{
object b = null; CodeDomProvider p = new CSharpCodeProvider(new Dictionary<string, string>() { { "CompilerVersion", "v3.5" } });
CompilerParameters cp = new CompilerParameters();
cp.ReferencedAssemblies.Add("System.dll");
cp.ReferencedAssemblies.Add("System.Configuration.dll");
cp.ReferencedAssemblies.Add("System.Core.dll");
cp.ReferencedAssemblies.Add("System.Data.dll");
cp.ReferencedAssemblies.Add("System.Data.DataSetExtensions.dll");
cp.ReferencedAssemblies.Add("System.Data.Entity.dll");
cp.ReferencedAssemblies.Add("System.Drawing.dll");
cp.ReferencedAssemblies.Add("System.EnterpriseServices.dll");
cp.ReferencedAssemblies.Add("System.Runtime.Serialization.dll");
cp.ReferencedAssemblies.Add("System.Security.dll");
cp.ReferencedAssemblies.Add("System.Web.dll");
cp.ReferencedAssemblies.Add("System.Web.Extensions.dll");
cp.ReferencedAssemblies.Add("System.Web.Mobile.dll");
cp.ReferencedAssemblies.Add("System.Web.Services.dll");
cp.ReferencedAssemblies.Add("System.Xml.dll");
cp.ReferencedAssemblies.Add("System.Xml.Linq.dll");
cp.GenerateInMemory = true;
cp.IncludeDebugInformation = false; CompilerResults cr = p.CompileAssemblyFromSource(cp, GenerateCode(str)); if (cr.Errors.HasErrors)
{
foreach (CompilerError err in cr.Errors)
{
b = "编译错误:" + err.ErrorText;
}
}
else
{ Assembly objAssembly = cr.CompiledAssembly;
object objHelloWorld = objAssembly.CreateInstance("CMS.DiyCS");
MethodInfo objMI = objHelloWorld.GetType().GetMethod("OutPut"); b = objMI.Invoke(objHelloWorld, null);
}
return b.tostr();
}
调用方式:string a = ParseCode("if(true){str = 1;}else{str = 0;}");
输出:a=1