出自ProgWiki
用途
參考資料
程式碼
using System;
using System.CodeDom.Compiler;
using System.Reflection;
using System.Collections.Generic;
//參考資料
// 使用CSharpCodeProvider类编译C#代码
// http://blog.csdn.net/jxzhaogang/archive/2006/07/06/885105.aspx
// 程式碼執行
// http://forums.microsoft.com/MSDN-CHT/ShowPost.aspx?PostID=4115237&SiteID=14
namespace dotNetLang
{
//編譯的來源? 檔案或記憶體(放在String[] 中)
enum CodeFrom
{
Files,
Sources
};
class CSharpCode
{
/// <summary>
/// 編譯器版本("v2.0" 或 "v3.5")
/// </summary>
public static string CompilerVersion = "v2.0";
/// <summary>
/// 編譯到檔案
/// </summary>
/// <param name="strOutputFile"></param>
/// <param name="CodeSrc"></param>
/// <param name="FromType"></param>
/// <param name="strMainClass"></param>
/// <returns></returns>
public static string Compile(string strOutputFile, string[] CodeSrc, CodeFrom FromType, string strMainClass)
{
Dictionary<string, string> providerOptions = new Dictionary<string, string>();
providerOptions.Add("CompilerVersion", CompilerVersion);
using (Microsoft.CSharp.CSharpCodeProvider CodeProvider = new Microsoft.CSharp.CSharpCodeProvider(providerOptions))
{
CompilerParameters param = new CompilerParameters();
//輸出的檔名
param.OutputAssembly = strOutputFile;
//預設引用mscorlib.dll
param.ReferencedAssemblies.Add(
System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory() + "mscorlib.dll");
//程式的入口的類別名稱
if (strMainClass != null)
param.MainClass = strMainClass;
//編譯時最佳化
param.CompilerOptions = "/optimize";
//宣告編譯成可執行檔 (只有可執行檔才有進入點)
param.GenerateExecutable = true;
//編譯程式碼
CompilerResults results;
if (FromType == CodeFrom.Sources)
results = CodeProvider.CompileAssemblyFromSource(param, CodeSrc);
else
results = CodeProvider.CompileAssemblyFromFile(param, CodeSrc);
//傳回檔名與路徑
if (results.Errors.HasErrors == false)
return results.PathToAssembly;
else
return null;
}
}
/// <summary>
/// 編譯到Assembly
/// </summary>
/// <param name="CodeSrc"></param>
/// <param name="FromType"></param>
/// <param name="strMainClass"></param>
/// <returns></returns>
public static Assembly Compile(string[] CodeSrc, CodeFrom FromType, string strMainClass)
{
Dictionary<string, string> providerOptions = new Dictionary<string, string>();
providerOptions.Add("CompilerVersion", CompilerVersion);
using (Microsoft.CSharp.CSharpCodeProvider CodeProvider = new Microsoft.CSharp.CSharpCodeProvider(providerOptions))
{
CompilerParameters param = new CompilerParameters();
//輸出在記憶體
param.GenerateInMemory = true;
//編譯時最佳化
param.CompilerOptions = "/optimize";
//預設引用mscorlib.dll
param.ReferencedAssemblies.Add(
System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory() + "mscorlib.dll");
//程式的入口的類別名稱
if (strMainClass != null)
param.MainClass = strMainClass;
//宣告編譯成可執行檔 (只有可執行檔才有進入點)
param.GenerateExecutable = true;
//編譯程式碼
CompilerResults results;
if (FromType == CodeFrom.Sources)
results = CodeProvider.CompileAssemblyFromSource(param, CodeSrc);
else
results = CodeProvider.CompileAssemblyFromFile(param, CodeSrc);
//如果沒有編譯錯誤回傳該組件
if (results.Errors.HasErrors == false)
return results.CompiledAssembly;
else
return null;
}
}
//public static void Exec(string strPathToAssembly)
//{
// AppDomain domain = AppDomain.CreateDomain("Compiled App");
// domain.ExecuteAssembly(strPathToAssembly);
//}
public static void Exec(string strPathToAssembly)
{
Assembly a = Assembly.Load(strPathToAssembly);
a.EntryPoint.Invoke(null, null); //如果Main()有參數的話 ,要改掉第2個null
}
public static void Exec(Assembly a)
{
a.EntryPoint.Invoke(null,null); //如果Main()有參數的話 ,要改掉第2個null
}
}
}