導航:首頁 > 源碼編譯 > net編譯運行原理

net編譯運行原理

發布時間:2025-05-12 02:34:43

1. 詳解.NET中的動態編譯技術

代碼的動態編譯並執行是一個 NET平台提供給我們的很強大的工具用以靈活擴展(當然是面對內部開發人員)復雜而無法估算的邏輯 並通過一些額外的代碼來擴展我們已有 的應用程序 這在很大程度上給我們提供了另外一種擴展的方式(當然這並不能算是嚴格意義上的擴展 但至少為我們提供了一種思路) 動態代碼執行可以應用在諸如模板生成 外加邏輯擴展等一些場合 一個簡單的例子 為了網站那的響應速度 HTML靜態頁面往往是我們最好的選擇 但基於數據驅動的網站往往又很難用靜態頁面實現 那麼將動態頁面生成的工作或許就是一個很好的應用場合 另外 對於一些模板的套用 我們同樣可以用它來做 另外這本身也是插件編寫的方式

最基本的動態編譯

Net為我們提供了很強大的支持來實現這一切我們可以去做的基礎 主要應用的兩個命名空間是 System CodeDom Compiler和Microsoft CSharp或Microsoft VisualBasic 另外還需要用到反射來動態執行你的代碼 動態編譯並執行代碼的原理其實在於將提供的源代碼交予CSharpCodeProvider來執行編譯(其實和CSC沒什麼兩樣) 如果沒有任何編譯錯誤 生成的IL代碼會被編譯成DLL存放於於內存並載入在某個應用程序域(默認為當前)內並通過反射的方式來調用其某個方法或者觸發某個事件等 之所以說它是插件編寫的一種方式也正是因為與此 我們可以通過預先定義好的借口來組織和擴展我們的程序並將其交還給主程序去觸發 一個基本的動態編譯並執行代碼的步驟包括

· 將要被編譯和執行的代碼讀入並以字元串方式保存

· 聲明CSharpCodeProvider對象實例

· 調用CSharpCodeProvider實例的CompileAssemblyFromSource方法編譯

· 用反射生成被生成對象的實例(Assembly CreateInstance)

· 調用其方法

以下代碼片段包含了完整的編譯和執行過程

//get the code to pile string strSourceCode = this txtSource Text; // Create a new CSharpCodePrivoder instance CSharpCodeProvider objCSharpCodePrivoder = new CSharpCodeProvider(); // Sets the runtime piling parameters

by crating a new CompilerParameters instance CompilerParameters objCompilerParameters = new CompilerParameters(); objCompilerParameters ReferencedAssemblies Add( System dll ); objCompilerParameters ReferencedAssemblies Add( System Windows Forms dll ); objCompilerParameters GenerateInMemory = true; // CompilerResults: Complile the code snippet

by calling a method from the provider CompilerResults cr = objCSharpCodePrivoder

CompileAssemblyFromSource(objCompilerParameters

strSourceCode); if (cr Errors HasErrors) { string strErrorMsg = cr Errors Count ToString() + Errors: ; for (int x = ; x < cr Errors Count; x++) { strErrorMsg = strErrorMsg + Line: + cr Errors[x] Line ToString() + + cr Errors[x] ErrorText; } this txtResult Text = strErrorMsg; MessageBox Show( There were build erros please modify your code

Compiling Error ); return; } // Invoke the method by using Reflection Assembly objAssembly = cr CompiledAssembly; object objClass = objAssembly CreateInstance( Dynamicly HelloWorld ); if (objClass == null) { this txtResult Text = Error: + Couldn t load class ; return; } object[] objCodeParms = new object[ ]; objCodeParms[ ] = Allan ; string strResult = (string)objClass GetType() InvokeMember( GetTime BindingFlags InvokeMethod null objClass objCodeParms); this txtResult Text = strResult;

需要解釋的是 這里我們在傳遞編譯參數時設置了GenerateInMemory為true 這表明生成的DLL會被載入在內存中(隨後被默認引用入當前應用程序域) 在調用GetTime方法時我們需要加入參數 傳遞object類型的數組並通過Reflection的InvokeMember來調用 在創建生成的Assembly中的對象實例時 需要注意用到的命名空間是你輸入代碼的真實命名空間 以下是我們輸入的測試代碼(為了方便 所有的代碼都在外部輸入 動態執行時不做調整)

using System; namespace Dynamicly { public class HelloWorld { public string GetTime(string strName) { return Wele + strName +

Check in at + System DateTime Now ToString(); } } }

運行附件中提供的程序 可以很容易得到一下結果

閱讀全文

與net編譯運行原理相關的資料

熱點內容
卡爾曼濾波演算法書籍 瀏覽:761
安卓手機怎麼用愛思助手傳文件進蘋果手機上 瀏覽:837
安卓怎麼下載60秒生存 瀏覽:797
外向式文件夾 瀏覽:229
dospdf 瀏覽:425
怎麼修改騰訊雲伺服器ip 瀏覽:380
pdftoeps 瀏覽:487
為什麼鴻蒙那麼像安卓 瀏覽:730
安卓手機怎麼拍自媒體視頻 瀏覽:180
單片機各個中斷的初始化 瀏覽:718
python怎麼集合元素 瀏覽:475
python逐條解讀 瀏覽:827
基於單片機的濕度控制 瀏覽:493
ios如何使用安卓的帳號 瀏覽:877
程序員公園采訪 瀏覽:805
程序員實戰教程要多長時間 瀏覽:968
企業數據加密技巧 瀏覽:129
租雲伺服器開發 瀏覽:807
程序員告白媽媽不同意 瀏覽:330
攻城掠地怎麼查看伺服器 瀏覽:595