You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
211 lines
6.2 KiB
211 lines
6.2 KiB
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<string> _lsRef;
|
|
private string _nameSpace;
|
|
private List<string> _lsUsing;
|
|
private string _className;
|
|
private string _methodName;
|
|
private string _strCode;
|
|
|
|
public List<string> lsUsing
|
|
{
|
|
get { return _lsUsing; }
|
|
set { _lsUsing = value; }
|
|
}
|
|
public List<string> 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
|
|
|
|
/// <summary>
|
|
/// ¹¹Ô캯Êý
|
|
/// </summary>
|
|
public Compiler()
|
|
{
|
|
this.lsUsing = new List<string>();
|
|
this.lsRef = new List<string>();
|
|
|
|
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");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Éú³É´úÂë
|
|
/// </summary>
|
|
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();
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// ±àÒë´úÂë
|
|
/// </summary>
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Ö´ÐдúÂë
|
|
/// </summary>
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// ´úÂ붯ִ̬ÐÐ
|
|
/// </summary>
|
|
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;
|
|
}
|
|
}
|
|
}
|