




下載本文檔
版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、程序集和全局程序集緩存(c#編程指南)程序集是任何.net framework應用程序的基本構造塊。例如,在生成簡單的c#應用程序 吋,visual studio創(chuàng)建一個單個可移植可執(zhí)行(pe)文件形式的程序集,明確地說就是一 個exe或dll。程序集包含描述它們自己的內部版本號和它們包含的所有數(shù)據(jù)和對象類型 的詳細信息的元數(shù)據(jù)。程序集僅在需要時才加載。如果不使用程序集,則不會加載。這意味 著程序集可能是在大型項目屮管理資源的有效途徑。程序集可以包含一個或多個模塊。例如,計劃較大的項目時,可以讓兒個各個開發(fā)人員負責 單獨的模塊,并通過組合所有這些模塊來創(chuàng)建單個程序集。程序集概述程序集具有以下特
2、點:程序集作為.exe或.dll文件實現(xiàn)。通過將程序集放在全局程序集緩存中,可在多個應用程序之i'可共亨程序集。要將程序集放在全局程序集緩存中,必須對程序集進行強命名。程序集僅在需要時才加載到內存中??梢允褂梅瓷鋪硪跃幊谭绞将@取關于程序集的信息。 如果加載程序集的目的只是對其進行檢查,應使用諸如reflectiononlyloadfrom 的方法??梢栽趩蝹€應用程序中使用相同程序集的兩個版本。創(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#復制代碼/ 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#復制代碼esc /1:module stringer. cs使用/t:編譯器選項指定module參數(shù),表明文件應作為模塊(而不是作為程序集)編譯。 編譯器生成名為stringer, netmodule的模塊,該模塊可添加到程序集。在上述過程的笫二步中,必須編譯包含對其他模塊的引用的模塊。此步驟使用/addmodule 編譯器選項。在下面的示例中,名為client的代碼模塊具有入口點mai
5、n方法,此方法引 用步驟1中創(chuàng)建的stringer, dll模塊中的方法。下面的示例說明了 client的代碼。c#復制代碼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#復制代碼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#心復制代碼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)建一個包含一個命名空間和兩個類的程序集。假設此程序集已經(jīng)生 成,并以helloworldremote
10、. exe為名存儲在驅動器c上。c#召復制代碼/ 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,);|為了從其他應用程序訪問該代碼,可以將該程序集加載到當前應用程序域中,或創(chuàng)建新的應 用程序域并將該程序集加載到其川。如杲使用assembly. loadfrom將程序集加載到當前應 用程序域中,您可以使用assembly. createlnstance來實例化remoteobject類的實例, 這樣將導致執(zhí)行對象構造函數(shù)。c#卻復制代碼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");|將程序集加載到一個單獨的應用程序域時,應使用appdomain. executeassembly來訪問默 認入口點,或使用appdomain. createlnstance創(chuàng)建remoteobject類的實例。創(chuàng)建該實 例將
13、導致執(zhí)行構造函數(shù)。c#也復制代碼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。然后向應用程序的using塊中添加一個using holloworldrcmote;指令,并在程序中使用remoteobject類型來聲明remoteobject對 彖的一個實例,如下所示:c#總復制代碼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應當僅在需要吋才將程序集安裝到全局程序集緩存中以進行共亨。一般原則是:程序集依賴 項保持專用,并在應用程序目錄中定位程序集,除非明確要求共享程序集。另外,不必為了 使com interop或非托管代碼可以訪問程序集而將程序集安裝到全局程序集緩存。 有若干方法可以將程序集部署到全局程序集緩存中: 使用專用于全局程序集緩存的安裝程序。該方法是將程序集
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 大型設備運輸合同書范本
- 團隊遴選流程標準化實施框架
- 常用機床電氣設備維修01?CA6140型臥式車床電氣控制線路維修glq
- 化學思維導圖培訓
- 紀錄片拍攝全流程解析
- 國培新教師培訓成果匯報
- 2025年貴州省中考英語試卷真題及答案詳解(精校打印版)
- 腫瘤內科工作總結
- 天真教育好好玩音樂培訓
- 愛國英雄美術課件
- 《弘揚教育家精神》專題課件
- 生豬屠宰獸醫(yī)衛(wèi)生檢驗人員理論考試題及答案
- 《人文英語4》形考任務(1-8)試題答案解析
- 北京市通州區(qū)2024-2025學年四年級語文下學期期末試卷新人教版
- 廣東省珠海市金灣區(qū)2023-2024學年八年級下學期7月期末歷史試題
- 社會語言學視角下網(wǎng)絡流行用語研究
- 數(shù)據(jù)庫程序設計智慧樹知到期末考試答案章節(jié)答案2024年外交學院
- 中職數(shù)學單招一輪總復習《集合》復習課件
- 設計投標服務方案
- 外來醫(yī)療器械清洗消毒
- 內科學(廣東藥科大學)智慧樹知到期末考試答案2024年
評論
0/150
提交評論