|
動(dòng)態(tài)代碼執(zhí)行可以應(yīng)用在諸如模板生成,外加邏輯擴(kuò)展等一些場(chǎng)合。一個(gè)簡(jiǎn)單的例子,為了網(wǎng)站那的響應(yīng)速度,HTML靜態(tài)頁(yè)面往往是我們最好的選擇,但基于數(shù)據(jù)驅(qū)動(dòng)的網(wǎng)站往往又很難用靜態(tài)頁(yè)面實(shí)現(xiàn),那么將動(dòng)態(tài)頁(yè)面生成html的工作或許就是一個(gè)很好的應(yīng)用場(chǎng)合。另外,對(duì)于一些模板的套用,我們同樣可以用它來(lái)做。另外這本身也是插件編寫(xiě)的方式。
最基本的動(dòng)態(tài)編譯
.NET為我們提供了很強(qiáng)大的支持來(lái)實(shí)現(xiàn)這一切我們可以去做的基礎(chǔ),主要應(yīng)用的兩個(gè)命名空間是:System.CodeDom.Compiler和Microsoft.CSharp或Microsoft.VisualBasic。另外還需要用到反射來(lái)動(dòng)態(tài)執(zhí)行你的代碼。動(dòng)態(tài)編譯并執(zhí)行代碼的原理其實(shí)在于將提供的源代碼交予CSharpCodeProvider來(lái)執(zhí)行編譯(其實(shí)和CSC沒(méi)什么兩樣),如果沒(méi)有任何編譯錯(cuò)誤,生成的IL代碼會(huì)被編譯成DLL存放于于內(nèi)存并加載在某個(gè)應(yīng)用程序域(默認(rèn)為當(dāng)前)內(nèi)并通過(guò)反射的方式來(lái)調(diào)用其某個(gè)方法或者觸發(fā)某個(gè)事件等。之所以說(shuō)它是插件編寫(xiě)的一種方式也正是因?yàn)榕c此,我們可以通過(guò)預(yù)先定義好的借口來(lái)組織和擴(kuò)展我們的程序并將其交還給主程序去觸發(fā)。一個(gè)基本的動(dòng)態(tài)編譯并執(zhí)行代碼的步驟包括:
? 將要被編譯和執(zhí)行的代碼讀入并以字符串方式保存
? 聲明CSharpCodeProvider對(duì)象實(shí)例
? 調(diào)用CSharpCodeProvider實(shí)例的CompileAssemblyFromSource方法編譯
? 用反射生成被生成對(duì)象的實(shí)例(Assembly.CreateInstance)
? 調(diào)用其方法
以下代碼片段包含了完整的編譯和執(zhí)行過(guò)程:
//get the code to compile string strSourceCode = this.txtSource.Text;
// 1.Create a new CSharpCodePrivoder instance CSharpCodeProvider objCSharpCodePrivoder = new CSharpCodeProvider();
// 2.Sets the runtime compiling 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;
// 3.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 = 0; x < cr.Errors.Count; x++) { strErrorMsg = strErrorMsg + "/r/nLine: " + 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; }
// 4. 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[1]; objCodeParms[0] = "Allan.";
string strResult = (string)objClass.GetType().InvokeMember( "GetTime", BindingFlags.InvokeMethod, null, objClass, objCodeParms);
this.txtResult.Text = strResult; |
需要解釋的是,這里我們?cè)趥鬟f編譯參數(shù)時(shí)設(shè)置了GenerateInMemory為true,這表明生成的DLL會(huì)被加載在內(nèi)存中(隨后被默認(rèn)引用入當(dāng)前應(yīng)用程序域)。在調(diào)用GetTime方法時(shí)我們需要加入?yún)?shù),傳遞object類型的數(shù)組并通過(guò)Reflection的InvokeMember來(lái)調(diào)用。在創(chuàng)建生成的Assembly中的對(duì)象實(shí)例時(shí),需要注意用到的命名空間是你輸入代碼的真實(shí)命名空間。以下是我們輸入的測(cè)試代碼(為了方便,所有的代碼都在外部輸入,動(dòng)態(tài)執(zhí)行時(shí)不做調(diào)整):
using System;
namespace Dynamicly { public class HelloWorld { public string GetTime(string strName) { return "Welcome " + strName + ", Check in at " + System.DateTime.Now.ToString(); } } } |
運(yùn)行附件中提供的程序,可以很容易得到一下結(jié)果:
改進(jìn)的執(zhí)行過(guò)程
現(xiàn)在一切看起來(lái)很好,我們可以編譯代碼并把代碼加載到當(dāng)前應(yīng)用程序域中來(lái)參與我們的活動(dòng),但你是否想過(guò)去卸載掉這段程序呢?更好的去控制程序呢?另外,當(dāng)你運(yùn)行這個(gè)程序很多遍的時(shí)候,你會(huì)發(fā)現(xiàn)占用內(nèi)存很大,而且每次執(zhí)行都會(huì)增大內(nèi)存使用。是否需要來(lái)解決這個(gè)問(wèn)題呢?當(dāng)然需要,否則你會(huì)發(fā)現(xiàn)這個(gè)東西根本沒(méi)用,我需要執(zhí)行的一些大的應(yīng)用會(huì)讓我的服務(wù)器crzay,不堪重負(fù)而瘋掉的。
要解決這個(gè)問(wèn)題我們需要來(lái)了解一下應(yīng)用程序域。.NET Application Domain是.NET提供的運(yùn)行和承載一個(gè)活動(dòng)的進(jìn)程(Process)的容器,它將這個(gè)進(jìn)程運(yùn)行所需的代碼和數(shù)據(jù),隔離到一個(gè)小的范圍內(nèi),稱為Application Domain。當(dāng)一個(gè)應(yīng)用程序運(yùn)行時(shí),Application Domains將所有的程序集/組件集加載到當(dāng)前的應(yīng)用程序域中,并根據(jù)需要來(lái)調(diào)用。而對(duì)于動(dòng)態(tài)生成的代碼/程序集,我們看起來(lái)好像并沒(méi)有辦法去管理它。其實(shí)不然,我們可以用Application Domain提供的管理程序集的辦法來(lái)動(dòng)態(tài)加載和移除Assemblies來(lái)達(dá)到我們的提高性能的目的。具體怎么做呢,在前邊的基礎(chǔ)上增加以下步驟:
? 創(chuàng)建另外一個(gè)Application Domain
? 動(dòng)態(tài)創(chuàng)建(編譯)代碼并保存到磁盤(pán)
? 創(chuàng)建一個(gè)公共的遠(yuǎn)程調(diào)用接口
? 創(chuàng)建遠(yuǎn)程調(diào)用接口的實(shí)例。并通過(guò)這個(gè)接口來(lái)訪問(wèn)其方法。
換句話來(lái)講就是將對(duì)象加載到另外一個(gè)AppDomain中并通過(guò)遠(yuǎn)程調(diào)用的方法來(lái)調(diào)用。所謂遠(yuǎn)程調(diào)用其實(shí)也就是跨應(yīng)用程序域調(diào)用,所以這個(gè)對(duì)象(動(dòng)態(tài)代碼)必須繼承于MarshalByRefObject類。為了復(fù)用,這個(gè)接口被單獨(dú)提到一個(gè)工程中,并提供一個(gè)工廠來(lái)簡(jiǎn)化每次的調(diào)用操作:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection;
namespace RemoteAccess { /// <summary> /// Interface that can be run over the remote AppDomain boundary. /// </summary> public interface IRemoteInterface { object Invoke(string lcMethod,object[] Parameters); }
/// <summary> /// Factory class to create objects exposing IRemoteInterface /// </summary> public class RemoteLoaderFactory : MarshalByRefObject { private const BindingFlags bfi = BindingFlags.Instance | BindingFlags.Public | BindingFlags.CreateInstance;
public RemoteLoaderFactory() {}
public IRemoteInterface Create( string assemblyFile, string typeName, object[] constructArgs ) { return (IRemoteInterface) Activator.CreateInstanceFrom( assemblyFile, typeName, false, bfi, null, constructArgs, null, null, null ).Unwrap(); } } } |
接下來(lái)在原來(lái)基礎(chǔ)上需要修改的是:
? 將編譯成的DLL保存到磁盤(pán)中。
? 創(chuàng)建另外的AppDomain。
? 獲得IRemoteInterface接口的引用。(將生成的DLL加載到額外的AppDomain)
? 調(diào)用InvokeMethod方法來(lái)遠(yuǎn)程調(diào)用。
? 可以通過(guò)AppDomain.Unload()方法卸載程序集。
以下是完整的代碼,演示了如何應(yīng)用這一方案。
//get the code to compile string strSourceCode = this.txtSource.Text;
//1. Create an addtional AppDomain AppDomainSetup objSetup = new AppDomainSetup(); objSetup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory; AppDomain objAppDomain = AppDomain.CreateDomain("MyAppDomain", null, objSetup);
// 1.Create a new CSharpCodePrivoder instance CSharpCodeProvider objCSharpCodePrivoder = new CSharpCodeProvider();
// 2.Sets the runtime compiling parameters by crating a new CompilerParameters instance CompilerParameters objCompilerParameters = new CompilerParameters(); objCompilerParameters.ReferencedAssemblies.Add("System.dll"); objCompilerParameters.ReferencedAssemblies.Add("System.Windows.Forms.dll");
// Load the remote loader interface objCompilerParameters.ReferencedAssemblies.Add("RemoteAccess.dll");
// Load the resulting assembly into memory objCompilerParameters.GenerateInMemory = false; objCompilerParameters.OutputAssembly = "DynamicalCode.dll";
// 3.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 = 0; x < cr.Errors.Count; x++) { strErrorMsg = strErrorMsg + "/r/nLine: " + 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; }
// 4. Invoke the method by using Reflection RemoteLoaderFactory factory = (RemoteLoaderFactory)objAppDomain.CreateInstance("RemoteAccess","RemoteAccess.RemoteLoaderFactory").Unwrap();
// with help of factory, create a real 'LiveClass' instance object objObject = factory.Create("DynamicalCode.dll", "Dynamicly.HelloWorld", null);
if (objObject == null) { this.txtResult.Text = "Error: " + "Couldn't load class."; return; }
// *** Cast object to remote interface, avoid loading type info IRemoteInterface objRemote = (IRemoteInterface)objObject;
object[] objCodeParms = new object[1]; objCodeParms[0] = "Allan.";
string strResult = (string)objRemote.Invoke("GetTime", objCodeParms);
this.txtResult.Text = strResult;
//Dispose the objects and unload the generated DLLs. objRemote = null; AppDomain.Unload(objAppDomain);
System.IO.File.Delete("DynamicalCode.dll"); |
對(duì)于客戶端的輸入程序,我們需要繼承于MarshalByRefObject類和IRemoteInterface接口,并添加對(duì)RemoteAccess程序集的引用。以下為輸入:
using System; using System.Reflection; using RemoteAccess;
namespace Dynamicly { public class HelloWorld : MarshalByRefObject,IRemoteInterface { public object Invoke(string strMethod,object[] Parameters) { return this.GetType().InvokeMember(strMethod, BindingFlags.InvokeMethod,null,this,Parameters); }
public string GetTime(string strName) { return "Welcome " + strName + ", Check in at " + System.DateTime.Now.ToString(); } } } |
這樣,你可以通過(guò)適時(shí)的編譯,加載和卸載程序集來(lái)保證你的程序始終處于一個(gè)可控消耗的過(guò)程,并且達(dá)到了動(dòng)態(tài)編譯的目的,而且因?yàn)樵诓煌?a href=/pingce/yingyong/ target=_blank class=infotextkey>應(yīng)用程序域中,讓你的本身的程序更加安全和健壯。示例代碼下載:
http://xiazai.jb51.NET/200905/yuanma/DynamicCompiler.rar
AspNet技術(shù):.NET 動(dòng)態(tài)編譯,轉(zhuǎn)載需保留來(lái)源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。