using System; using System.Collections.Generic; using System.Text; using System.Reflection; using Microsoft.CSharp; using System.CodeDom; using System.CodeDom.Compiler; namespace SiaSun.LMS.Common { public class Compiler { #region ------定义变量和属性 private List _lsRef; private string _nameSpace; private List _lsUsing; private string _className; private string _methodName; private string _strCode; public List lsUsing { get { return _lsUsing; } set { _lsUsing = value; } } public List lsRef { get { return _lsRef; } set { _lsRef = value; } } public string nameSpace { get { return _nameSpace; } set { _nameSpace = value; } } public string className { get { return _className; } set { _className = value; } } public string methodName { get { return _methodName; } set { _methodName = value; } } public string strCode { get { return _strCode; } set { _strCode = value; } } #endregion /// /// 构造函数 /// public Compiler() { this.lsUsing = new List(); this.lsRef = new List(); this._nameSpace = "SiaSun.LMS.UI.Compiler"; this._className = "Complier"; this._methodName = "OutPut"; this.lsUsing.Add("System"); this.lsUsing.Add("System.CodeDom.Compiler"); this.lsRef.Add("System.dll"); this.lsUsing.Add("System.Reflection"); this.lsRef.Add("System.Data.dll"); this.lsRef.Add("System.Xml.dll"); } /// /// 生成代码 /// public string GenerateCode() { StringBuilder sb = new StringBuilder(); for (int i = 0; i < this.lsUsing.Count; i++) { sb.Append(string.Format("using {0};", this.lsUsing[i])); sb.Append(Environment.NewLine); } sb.Append("namespace ").Append(nameSpace); sb.Append(Environment.NewLine); sb.Append("{"); sb.Append(Environment.NewLine); sb.Append("public class ").Append(className); sb.Append(Environment.NewLine); sb.Append("{"); sb.Append(Environment.NewLine); sb.Append(string.Format("public bool {0}(out string sResult)", this._methodName)); sb.Append(Environment.NewLine); sb.Append("{"); sb.Append(Environment.NewLine); sb.Append("bool bResult=true;"); sb.Append(Environment.NewLine); sb.Append("sResult=string.Empty;"); sb.Append(Environment.NewLine); sb.Append(strCode); sb.Append(Environment.NewLine); sb.Append("return bResult;"); sb.Append(Environment.NewLine); sb.Append("}"); sb.Append(Environment.NewLine); sb.Append("}"); sb.Append(Environment.NewLine); sb.Append("}"); return sb.ToString(); } /// /// 编译代码 /// public CompilerResults CompilerCode() { CSharpCodeProvider compilerPrivoder = new CSharpCodeProvider(); //设置编译参数和属性 CompilerParameters compilerParameters = new CompilerParameters(); for (int i = 0; i < lsRef.Count; i++) { compilerParameters.ReferencedAssemblies.Add(lsRef[i]); } compilerParameters.GenerateExecutable = false; compilerParameters.GenerateInMemory = true; CompilerResults results = compilerPrivoder.CompileAssemblyFromSource(compilerParameters, GenerateCode()); return results; } /// /// 执行代码 /// public bool ExecuteCode(CompilerResults cResult, string methodName, out string sResult) { bool bResult = true; sResult = string.Empty; try { //反射 Assembly complierAssembly = cResult.CompiledAssembly; //创建类实例 object complierInstance = complierAssembly.CreateInstance(nameSpace + "." + className); //创建方法 MethodInfo complierMethod = complierInstance.GetType().GetMethod(methodName); //调用方法 string result = string.Empty; object[] obj = new object[] { result }; bResult = Convert.ToBoolean(complierMethod.Invoke(complierInstance, obj)); sResult = obj[0].ToString(); } catch (Exception ex) { bResult = false; sResult = ex.Message; } return bResult; } /// /// 代码动态执行 /// public bool Excute(out string sResult) { bool bResult = true; sResult = string.Empty; try { string strGenerateCode = this.GenerateCode(); //获得编译结果 CompilerResults results = this.CompilerCode(); //查看编译结果 if (results.Errors.HasErrors) { string strError = string.Empty; foreach (CompilerError ex in results.Errors) { strError += ex.ErrorText; } bResult = false; sResult = strError; return bResult; } //调用执行方法 bResult = this.ExecuteCode(results, this._methodName, out sResult); } catch (Exception ex) { bResult = false; sResult = ex.Message; } return bResult; } } }