




下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)
文檔簡介
1、程序集和全局程序集緩存(c#編程指南)程序集是任何.net framework應(yīng)用程序的基本構(gòu)造塊。例如,在生成簡單的c#應(yīng)用程序 吋,visual studio創(chuàng)建一個單個可移植可執(zhí)行(pe)文件形式的程序集,明確地說就是一 個exe或dll。程序集包含描述它們自己的內(nèi)部版本號和它們包含的所有數(shù)據(jù)和對象類型 的詳細信息的元數(shù)據(jù)。程序集僅在需要時才加載。如果不使用程序集,則不會加載。這意味 著程序集可能是在大型項目屮管理資源的有效途徑。程序集可以包含一個或多個模塊。例如,計劃較大的項目時,可以讓兒個各個開發(fā)人員負責(zé) 單獨的模塊,并通過組合所有這些模塊來創(chuàng)建單個程序集。程序集概述程序集具有以下特
2、點:程序集作為.exe或.dll文件實現(xiàn)。通過將程序集放在全局程序集緩存中,可在多個應(yīng)用程序之i'可共亨程序集。要將程序集放在全局程序集緩存中,必須對程序集進行強命名。程序集僅在需要時才加載到內(nèi)存中??梢允褂梅瓷鋪硪跃幊谭绞将@取關(guān)于程序集的信息。 如果加載程序集的目的只是對其進行檢查,應(yīng)使用諸如reflectiononlyloadfrom 的方法??梢栽趩蝹€應(yīng)用程序中使用相同程序集的兩個版本。創(chuàng)建多文件程序集用于c#和visual basic的visual studio 2005 ide只能用于創(chuàng)建單文件程序集。如果 要創(chuàng)建多文件程序集,必須使用命令行編譯器或帶有visual c卄 的
3、visual studio 2005o 下面的示例通過編譯包含其他文件所引用的命名空間的文件,來闡釋上述過程的步驟1。此 示例開始時是stringer文件的一些簡單代碼,stringer具有名為mystringer的命名空 間(帶有名為stringer的類)。stringer類包含名為stringermethod的方法,此方法 將單獨一行寫入控制臺。c#復(fù)制代碼/ assembly building example in the . net framework.using system;namespacc mystringerpublic class stringerpublic void s
4、tringermethod()system. console. writelinec'this is a line from stringetmethod.");使用下面的命令編譯此代碼:c#復(fù)制代碼esc /1:module stringer. cs使用/t:編譯器選項指定module參數(shù),表明文件應(yīng)作為模塊(而不是作為程序集)編譯。 編譯器生成名為stringer, netmodule的模塊,該模塊可添加到程序集。在上述過程的笫二步中,必須編譯包含對其他模塊的引用的模塊。此步驟使用/addmodule 編譯器選項。在下面的示例中,名為client的代碼模塊具有入口點mai
5、n方法,此方法引 用步驟1中創(chuàng)建的stringer, dll模塊中的方法。下面的示例說明了 client的代碼。c#復(fù)制代碼using system;using mystringer; /the namespace created in stringer. netmodule.class mainclientapp/ static method main is the entry point method.public static void main()stringer mystringinstancc = new stringer();con sole. writeline (,zc1 i
6、e nt code executes");/mystringcomp. stri nger();mystringlnstanee. stringermethod();使用下面的命令編譯此代碼:c#復(fù)制代碼esc /addmodule:stringer, netmodule /t:module client, cs指圧/t:module選項,因為此模塊將在以后的步驟中添加到程序集。指定/addmodule選 項,因為client中的代碼引用stringer, netmodule中的代碼創(chuàng)建的命名空i'可。編譯器生 成名為cl ient. netmodule的模塊,它包含對另一模
7、塊stringer, netmodule的引用。兩次編譯創(chuàng)建出一個雙文件程序集:esc /t:module stringer, csesc client.es /addmodule:stringer. netmodule 一次編譯創(chuàng)建出一個雙文件程序集:esc /out:client. exe client. cs /out:smodule stringer.cs此示例測試一個dll以確定它是否為程序集。c#心復(fù)制代碼class testassemblystatic void main()trysystem. reflection. assemblyname testassembly -sys
8、tem. ref lection. assemblyxame. getassemblyxame c: windows systemavicap. dll" );system. console. writeline("yes, the file is an assembly.z/);catch (system. to. fi1enotfoundexception e)system. console. writeline (z/the file can not be found.z/);catch (system. badtmageformatexception e)syste
9、m. console. writeline (z/the file is not an assembly. z,);catch (system. to. fileloadexception e)system. console. writeline("the assembly has already been loaded.,z);getassemblyname方法加載測試文件,然后在讀取信息之后釋放它。 輸出the file is not an assembly.請考慮下而的文件,它創(chuàng)建一個包含一個命名空間和兩個類的程序集。假設(shè)此程序集已經(jīng)生 成,并以helloworldremote
10、. exe為名存儲在驅(qū)動器c上。c#召復(fù)制代碼/ this namespace contains code to be cal space hellowor1dremotepublic class remoteobject : system. marshalbyrefobjectpublic remoteobject()system. console. writeline("hello, world! (remoteobjectconstructor)“);class programstatic void main()system. console. writelin
11、e (z,hello, worl d! (main method)z,);|為了從其他應(yīng)用程序訪問該代碼,可以將該程序集加載到當(dāng)前應(yīng)用程序域中,或創(chuàng)建新的應(yīng) 用程序域并將該程序集加載到其川。如杲使用assembly. loadfrom將程序集加載到當(dāng)前應(yīng) 用程序域中,您可以使用assembly. createlnstance來實例化remoteobject類的實例, 這樣將導(dǎo)致執(zhí)行對象構(gòu)造函數(shù)。c#卻復(fù)制代碼static void maino/ load the assembly into the current appdomain:system. reflection. assembly n
12、ewassembly =systcm. ref lection. assembly. loadfrom(,zc:hc11 oworidremote. exe");/ instantiate remoteobject:new a ssembly. create in sta nceclielloworldremote. rem ot eobject");|將程序集加載到一個單獨的應(yīng)用程序域時,應(yīng)使用appdomain. executeassembly來訪問默 認(rèn)入口點,或使用appdomain. createlnstance創(chuàng)建remoteobject類的實例。創(chuàng)建該實 例將
13、導(dǎo)致執(zhí)行構(gòu)造函數(shù)。c#也復(fù)制代碼static void mainosystem. appdomain newappdomain =system. appdomain. createdomai n("newappli cationdomai n");/ load the assembly and call the default entry point:newappdomain. executeasscmb 1 y (,zc:he 11owor 1 dremote. exez,);/ create an instance of remoteobject:newappdomai
14、n. createtnstancefrom("c:iiel1oworldremote.exe",z,hel 1 oworl dremote. remoteobject");i如果不想以編程方式加載程序集,可以從“解決方案資源管理器”中使用“添加引用”來指 定程序集helloworldremote.exe。然后向應(yīng)用程序的using塊中添加一個using holloworldrcmote;指令,并在程序中使用remoteobject類型來聲明remoteobject對 彖的一個實例,如下所示:c#總復(fù)制代碼static void maino/ this code c
15、reates an instancc of remoteobject, assumi ng helloworldremote has been added as a reference:hel1owor1dremote. remoteobject o 二 new helloworldremote. remoteobject();i應(yīng)當(dāng)僅在需要吋才將程序集安裝到全局程序集緩存中以進行共亨。一般原則是:程序集依賴 項保持專用,并在應(yīng)用程序目錄中定位程序集,除非明確要求共享程序集。另外,不必為了 使com interop或非托管代碼可以訪問程序集而將程序集安裝到全局程序集緩存。 有若干方法可以將程序集部署到全局程序集緩存中: 使用專用于全局程序集緩存的安裝程序。該方法是將程序集
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 安全員c證試題及答案
- 2023年度“安全生產(chǎn)事故隱患排查”知識競賽題庫試題附答案詳解
- 2024年“安全生產(chǎn)事故隱患排查”知識競賽全真模擬模擬題帶答案詳解
- 防暑降溫創(chuàng)新案例分享
- 建筑公司混凝土溫控?zé)o線傳感監(jiān)測制度
- 第1課時 平行與垂直 導(dǎo)學(xué)案 人教版數(shù)學(xué)四年級上冊
- 廣州版綜合實踐活動四年級下冊第二單元第1課《認(rèn)識博物館》教案
- 建筑公司攤鋪機施工平整度控制辦法
- 在自主游戲中推進幼兒深度學(xué)習(xí)
- 運用情景關(guān)系引領(lǐng)詩詞賞析
- 《拍賣概論》考試題庫(精煉版)
- DL-T5434-2021電力建設(shè)工程監(jiān)理規(guī)范
- 設(shè)計投標(biāo)服務(wù)方案
- “一帶一路”倡議與國際合作課件
- 貨物供應(yīng)方案及運輸方案
- 中醫(yī)養(yǎng)生健康小妙招的課件
- 拉鏈采購合同
- 紀(jì)檢監(jiān)察大數(shù)據(jù)平臺建設(shè)方案
- 09J202-1 坡屋面建筑構(gòu)造(一)-2
- 2024年山東兗礦能源集團股份有限公司招聘筆試參考題庫含答案解析
- 產(chǎn)能管理制度
評論
0/150
提交評論