CodeDomHelper.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.CSharp;
  6. using System.CodeDom.Compiler;
  7. using System.Reflection;
  8. namespace Bowin.Common.DynamicRun
  9. {
  10. public class CodeDomHelper
  11. {
  12. #region 执行静态代码
  13. /// <summary>
  14. /// 执行代码,返回结果(默认引用了System.dll、System.Data.dll、System.Xml.dll、System.Web.dll 4个程序集)
  15. /// </summary>
  16. /// <param name="code">代码</param>
  17. /// <param name="arrReferencedAssemblies">要引用的程序集</param>
  18. /// <returns></returns>
  19. public static object ExecuteCode(string code, string[] arrReferencedAssemblies)
  20. {
  21. CSharpCodeProvider objCSharpCodePrivoder = new CSharpCodeProvider();
  22. CompilerParameters objCompilerParameters = new CompilerParameters();
  23. objCompilerParameters.ReferencedAssemblies.Add("System.dll");
  24. //引用dll
  25. objCompilerParameters.ReferencedAssemblies.Add("System.Data.dll");
  26. //引用dll
  27. objCompilerParameters.ReferencedAssemblies.Add("System.Xml.dll");
  28. //引用dll
  29. objCompilerParameters.ReferencedAssemblies.Add("System.Web.dll");
  30. //引用dll
  31. if (arrReferencedAssemblies != null)
  32. foreach (string item in arrReferencedAssemblies)
  33. objCompilerParameters.ReferencedAssemblies.Add(item);
  34. objCompilerParameters.GenerateExecutable = false;
  35. objCompilerParameters.GenerateInMemory = true;
  36. CompilerResults cr = (objCSharpCodePrivoder.CompileAssemblyFromSource(objCompilerParameters,
  37. new [] { GenerateCode(code) }));
  38. if (cr.Errors.HasErrors)
  39. {
  40. StringBuilder l_err = new StringBuilder("编译错误:");
  41. foreach (CompilerError err in cr.Errors)
  42. {
  43. l_err.AppendLine(err.ErrorText);
  44. }
  45. return l_err.ToString();
  46. }
  47. // 通过反射,调用HelloWorld的实例
  48. Assembly objAssembly = cr.CompiledAssembly;
  49. object objHelloWorld = objAssembly.CreateInstance("Bowin.Common.CommonOtherDynamicCodeGenerateClass");
  50. MethodInfo objMI = objHelloWorld.GetType().GetMethod("OutPut");
  51. // 调用执行
  52. return objMI.Invoke(objHelloWorld, null);
  53. }
  54. /// <summary>
  55. /// 执行代码,返回结果(默认引用了System.dll、System.Data.dll、System.Xml.dll 3个程序集)
  56. /// </summary>
  57. /// <param name="code"></param>
  58. /// <returns></returns>
  59. public static object ExecuteCode(string code)
  60. {
  61. return ExecuteCode(code, null);
  62. }
  63. private static string GenerateCode(string code)
  64. {
  65. StringBuilder sb = new StringBuilder();
  66. sb.Append("using System;");
  67. sb.Append(Environment.NewLine);
  68. sb.Append("using System.Text;");
  69. sb.Append(Environment.NewLine);
  70. sb.Append("using System.IO;");
  71. sb.Append(Environment.NewLine);
  72. sb.Append("using System.Data;");
  73. sb.Append(Environment.NewLine);
  74. sb.Append("using System.Xml;");
  75. sb.Append(Environment.NewLine);
  76. sb.Append("using System.Web;");
  77. sb.Append(Environment.NewLine);
  78. sb.Append("using System.Collections;");
  79. sb.Append(Environment.NewLine);
  80. sb.Append("using System.Collections.Generic;");
  81. sb.Append(Environment.NewLine);
  82. sb.Append("namespace Bowin.Common");
  83. sb.Append(Environment.NewLine);
  84. sb.Append("{");
  85. sb.Append(Environment.NewLine);
  86. sb.Append(" public class CommonOtherDynamicCodeGenerateClass");
  87. sb.Append(Environment.NewLine);
  88. sb.Append(" {");
  89. sb.Append(Environment.NewLine);
  90. sb.Append(" public object OutPut()");
  91. sb.Append(Environment.NewLine);
  92. sb.Append(" {");
  93. sb.Append(Environment.NewLine);
  94. sb.Append(" return " + code + ";");
  95. sb.Append(Environment.NewLine);
  96. sb.Append(" }");
  97. sb.Append(Environment.NewLine);
  98. sb.Append(" }");
  99. sb.Append(Environment.NewLine);
  100. sb.Append("}");
  101. return sb.ToString();
  102. }
  103. #endregion
  104. }
  105. }