天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

.NET 動(dòng)態(tài)編譯

這在很大程度上給我們提供了另外一種擴(kuò)展的方式(當(dāng)然這并不能算是嚴(yán)格意義上的擴(kuò)展,但至少為我們提供了一種思路)。

動(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ì)于一些模板的套用,我們同樣可以用它來做。另外這本身也是插件編寫的方式。

 

最基本的動(dòng)態(tài)編譯

 

.NET為我們提供了很強(qiáng)大的支持來實(shí)現(xiàn)這一切我們可以去做的基礎(chǔ),主要應(yīng)用的兩個(gè)命名空間是:System.CodeDom.Compiler和Microsoft.CSharp或Microsoft.VisualBasic。另外還需要用到反射來動(dòng)態(tài)執(zhí)行你的代碼。動(dòng)態(tài)編譯并執(zhí)行代碼的原理其實(shí)在于將提供的源代碼交予CSharpCodeProvider來執(zhí)行編譯(其實(shí)和CSC沒什么兩樣),如果沒有任何編譯錯(cuò)誤,生成的IL代碼會(huì)被編譯成DLL存放于于內(nèi)存并加載在某個(gè)應(yīng)用程序域(默認(rèn)為當(dāng)前)內(nèi)并通過反射的方式來調(diào)用其某個(gè)方法或者觸發(fā)某個(gè)事件等。之所以說它是插件編寫的一種方式也正是因?yàn)榕c此,我們可以通過預(yù)先定義好的借口來組織和擴(kuò)展我們的程序并將其交還給主程序去觸發(fā)。一個(gè)基本的動(dòng)態(tài)編譯并執(zhí)行代碼的步驟包括:

?         將要被編譯和執(zhí)行的代碼讀入并以字符串方式保存

?         聲明CSharpCodeProvider對(duì)象實(shí)例

?         調(diào)用CSharpCodeProvider實(shí)例的CompileAssemblyFromSource方法編譯

?         用反射生成被生成對(duì)象的實(shí)例(Assembly.CreateInstance)

?         調(diào)用其方法

以下代碼片段包含了完整的編譯和執(zhí)行過程:

//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ù)組并通過Reflection的InvokeMember來調(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í)行過程

 

現(xiàn)在一切看起來很好,我們可以編譯代碼并把代碼加載到當(dāng)前應(yīng)用程序域中來參與我們的活動(dòng),但你是否想過去卸載掉這段程序呢?更好的去控制程序呢?另外,當(dāng)你運(yùn)行這個(gè)程序很多遍的時(shí)候,你會(huì)發(fā)現(xiàn)占用內(nèi)存很大,而且每次執(zhí)行都會(huì)增大內(nèi)存使用。是否需要來解決這個(gè)問題呢?當(dāng)然需要,否則你會(huì)發(fā)現(xiàn)這個(gè)東西根本沒用,我需要執(zhí)行的一些大的應(yīng)用會(huì)讓我的服務(wù)器crzay,不堪重負(fù)而瘋掉的。

要解決這個(gè)問題我們需要來了解一下應(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ù)需要來調(diào)用。而對(duì)于動(dòng)態(tài)生成的代碼/程序集,我們看起來好像并沒有辦法去管理它。其實(shí)不然,我們可以用Application Domain提供的管理程序集的辦法來動(dòng)態(tài)加載和移除Assemblies來達(dá)到我們的提高性能的目的。具體怎么做呢,在前邊的基礎(chǔ)上增加以下步驟:

?         創(chuàng)建另外一個(gè)Application Domain

?         動(dòng)態(tài)創(chuàng)建(編譯)代碼并保存到磁盤

?         創(chuàng)建一個(gè)公共的遠(yuǎn)程調(diào)用接口

?         創(chuàng)建遠(yuǎn)程調(diào)用接口的實(shí)例。并通過這個(gè)接口來訪問其方法。

換句話來講就是將對(duì)象加載到另外一個(gè)AppDomain中并通過遠(yuǎn)程調(diào)用的方法來調(diào)用。所謂遠(yuǎn)程調(diào)用其實(shí)也就是跨應(yīng)用程序域調(diào)用,所以這個(gè)對(duì)象(動(dòng)態(tài)代碼)必須繼承于MarshalByRefObject類。為了復(fù)用,這個(gè)接口被單獨(dú)提到一個(gè)工程中,并提供一個(gè)工廠來簡(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();

                   }

          }       

}

 

接下來在原來基礎(chǔ)上需要修改的是:

?         將編譯成的DLL保存到磁盤中。

?         創(chuàng)建另外的AppDomain。

?         獲得IRemoteInterface接口的引用。(將生成的DLL加載到額外的AppDomain)

?         調(diào)用InvokeMethod方法來遠(yuǎn)程調(diào)用。

?         可以通過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();

        }

    }

}

 

這樣,你可以通過適時(shí)的編譯,加載和卸載程序集來保證你的程序始終處于一個(gè)可控消耗的過程,并且達(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)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 6080伦理久久亚洲精品 | 99精品中文字幕在线观看 | 青青草偷拍国产亚洲欧洲 | 欧美不卡一区二区三区 | 久久婷五月综合色啪首页 | 国产一区二区免费在线观看 | 欧美性爱 成人 | 精子网久久国产精品 | 久久久久综合一本久道 | 久久夜色噜噜噜亚洲AV0000 | 美女扒开腿让男生桶爽免费APP | xvideos中文版在线视频 | YIN荡的老师系列第6部分视频 | 黄色三级在线 | 俄罗斯孩交精品 | 羞羞答答dc视频 | 中文字幕精品AV内射夜夜夜 | 99久久爱re热6在线播放 | 九九热这里只有精品视频免费 | 区久久AAA片69亚洲 | 牛牛在线1视频 | 亚洲AV久久无码精品国产网站 | 色橹橹欧美在线观看视频高清 | 伊人久久国产免费观看视频 | 理论片在线观看片免费 | 国产色婷婷精品人妻蜜桃成熟时 | 97色伦图片97色伦图影院久久 | 诱受H嗯啊巨肉各种play | 最新国产亚洲亚洲精品视频 | 蜜芽资源高清在线观看 | 总裁呻吟双腿大开男男H | 国产AV电影区二区三区曰曰骚网 | 国产免费怕怕免费视频观看 | 99久久精品费精品国产 | 亚洲成年人影院 | 亚洲国产欧美另类 | 国产成人综合网在线观看 | 国产精品亚洲精品日韩电影 | 国产成人8x视频一区二区 | 无码国产伦一区二区三区视频 | 国产色精品久久人妻无码看片软件 |