123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- 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 执行静态代码
- /// <summary>
- /// 执行代码,返回结果(默认引用了System.dll、System.Data.dll、System.Xml.dll、System.Web.dll 4个程序集)
- /// </summary>
- /// <param name="code">代码</param>
- /// <param name="arrReferencedAssemblies">要引用的程序集</param>
- /// <returns></returns>
- 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);
- }
-
- /// <summary>
- /// 执行代码,返回结果(默认引用了System.dll、System.Data.dll、System.Xml.dll 3个程序集)
- /// </summary>
- /// <param name="code"></param>
- /// <returns></returns>
- 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
- }
- }
|