using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.CSharp;
using System.CodeDom.Compiler;
using System.Reflection;
namespace Bowin.Common.DynamicRun
{
public class CodeDomHelper
{
#region 执行静态代码
///
/// 执行代码,返回结果(默认引用了System.dll、System.Data.dll、System.Xml.dll、System.Web.dll 4个程序集)
///
/// 代码
/// 要引用的程序集
///
public static object ExecuteCode(string code, string[] arrReferencedAssemblies)
{
CSharpCodeProvider objCSharpCodePrivoder = new CSharpCodeProvider();
CompilerParameters objCompilerParameters = new CompilerParameters();
objCompilerParameters.ReferencedAssemblies.Add("System.dll");
//引用dll
objCompilerParameters.ReferencedAssemblies.Add("System.Data.dll");
//引用dll
objCompilerParameters.ReferencedAssemblies.Add("System.Xml.dll");
//引用dll
objCompilerParameters.ReferencedAssemblies.Add("System.Web.dll");
//引用dll
if (arrReferencedAssemblies != null)
foreach (string item in arrReferencedAssemblies)
objCompilerParameters.ReferencedAssemblies.Add(item);
objCompilerParameters.GenerateExecutable = false;
objCompilerParameters.GenerateInMemory = true;
CompilerResults cr = (objCSharpCodePrivoder.CompileAssemblyFromSource(objCompilerParameters,
new [] { GenerateCode(code) }));
if (cr.Errors.HasErrors)
{
StringBuilder l_err = new StringBuilder("编译错误:");
foreach (CompilerError err in cr.Errors)
{
l_err.AppendLine(err.ErrorText);
}
return l_err.ToString();
}
// 通过反射,调用HelloWorld的实例
Assembly objAssembly = cr.CompiledAssembly;
object objHelloWorld = objAssembly.CreateInstance("Bowin.Common.CommonOtherDynamicCodeGenerateClass");
MethodInfo objMI = objHelloWorld.GetType().GetMethod("OutPut");
// 调用执行
return objMI.Invoke(objHelloWorld, null);
}
///
/// 执行代码,返回结果(默认引用了System.dll、System.Data.dll、System.Xml.dll 3个程序集)
///
///
///
public static object ExecuteCode(string code)
{
return ExecuteCode(code, null);
}
private static string GenerateCode(string code)
{
StringBuilder sb = new StringBuilder();
sb.Append("using System;");
sb.Append(Environment.NewLine);
sb.Append("using System.Text;");
sb.Append(Environment.NewLine);
sb.Append("using System.IO;");
sb.Append(Environment.NewLine);
sb.Append("using System.Data;");
sb.Append(Environment.NewLine);
sb.Append("using System.Xml;");
sb.Append(Environment.NewLine);
sb.Append("using System.Web;");
sb.Append(Environment.NewLine);
sb.Append("using System.Collections;");
sb.Append(Environment.NewLine);
sb.Append("using System.Collections.Generic;");
sb.Append(Environment.NewLine);
sb.Append("namespace Bowin.Common");
sb.Append(Environment.NewLine);
sb.Append("{");
sb.Append(Environment.NewLine);
sb.Append(" public class CommonOtherDynamicCodeGenerateClass");
sb.Append(Environment.NewLine);
sb.Append(" {");
sb.Append(Environment.NewLine);
sb.Append(" public object OutPut()");
sb.Append(Environment.NewLine);
sb.Append(" {");
sb.Append(Environment.NewLine);
sb.Append(" return " + code + ";");
sb.Append(Environment.NewLine);
sb.Append(" }");
sb.Append(Environment.NewLine);
sb.Append(" }");
sb.Append(Environment.NewLine);
sb.Append("}");
return sb.ToString();
}
#endregion
}
}