C# AppDomain_第1頁
C# AppDomain_第2頁
C# AppDomain_第3頁
C# AppDomain_第4頁
全文預覽已結束

付費下載

VIP免費下載

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、.NET   Framework   類庫     AppDomain   類     表示應用程序域,它是一個應用程序在其中執行的獨立環境。無法繼承此類。   命名空間:System 程序集:mscorlib(在   mscorlib.dll   中) 應用程序域(由   AppDomain   對象表示)為執行托管代碼提供隔離、卸載和安全邊界。 使用應用程序域隔離可能終止進程的任務。如果正在執行任務的  

2、; AppDomain   的狀態變得不穩定,則可以卸載   AppDomain,但不會影響進程。當進程必須不重新啟動而長時間運行時,這一點很重要。還可使用應用程序域隔離不應共享數據的任務。 如果程序集被加載到默認應用程序域中,則當進程運行時將無法從內存中卸載該程序集。但是,如果打開另一個應用程序域來加載和執行程序集,則卸載該應用程序域時也會同時卸載程序集。使用此技術最小化長時間運行的進程的工作集,這些進程偶爾會使用大型   DLL。   多個應用程序域可以在一個進程中運行;但是,在應用程序域和線程之間沒有一對一的關聯。多個線程可以屬

3、于一個應用程序域,盡管給定的線程并不局限于一個應用程序域,但在任何給定時間,線程都在一個應用程序域中執行。 應用程序域通過使用   CreateDomain   方法來創建。AppDomain   實例用于加載和執行程序集   (Assembly)。當不再使用   AppDomain   時,可以將它卸載。 AppDomain   類實現一組事件,這些事件使應用程序可以在加載程序集、要卸載應用程序域或引發未處理的異常時進行響應。 有關使用應用程序域的更多信息,請參見應用程序域。 此類實

4、現   MarshalByRefObject、_AppDomain   和   IEvidenceFactory   接口。 在任何情況下都不應創建   AppDomain   對象的可遠程控制的包裝。這樣做可發布對該   AppDomain   的遠程引用,將諸如   CreateInstance   方法向遠程訪問公開,并有效損壞該   AppDomain   的代碼訪問安全性。連接到遠程   AppDomain   的惡意客戶端可以獲得對

5、  AppDomain   本身可訪問的所有資源的訪問權。您不應為任何以下類型創建可遠程控制的包裝:擴展   MarshalByRefObject   的類型和實現惡意客戶端可用來繞過安全系統的方法的類型。 此示例顯示如何創建新的   AppDomain,在該新建   AppDomain   中實例化類型,以及與該類型的對象通信。此外,此示例還顯示如何卸載導致對象被垃圾回收的   AppDomain。 using   System; using   System.R

6、eflection; using   System.Threading; class   Module1          public   static   void   Main()                          /   Get   and   display  

7、the   friendly   name   of   the   default   AppDomain.                 string   callingDomainName   =   Thread.GetDomain().FriendlyName;                 Console.Wri

8、teLine(callingDomainName);                 /   Get   and   display   the   full   name   of   the   EXE   assembly.                 string   exeAssembly &#

9、160; =   Assembly.GetEntryAssembly().FullName;                 Console.WriteLine(exeAssembly);                 /   Construct   and   initialize   settings   for   a   sec

10、ond   AppDomain.                 AppDomainSetup   ads   =   new   AppDomainSetup();                 ads.ApplicationBase   =               &#

11、160;           "file:/ "   +   System.Environment.CurrentDirectory;                 ads.DisallowBindingRedirects   =   false;                 ads.DisallowC

12、odeDownload   =   true;                 ads.ConfigurationFile   =                           AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;    

13、             /   Create   the   second   AppDomain.                 AppDomain   ad2   =   AppDomain.CreateDomain( "AD   #2 ",   null,   ads);     

14、0;           /   Create   an   instance   of   MarshalbyRefType   in   the   second   AppDomain.                  /   A   proxy   to   the   object   is &#

15、160; returned.                 MarshalByRefType   mbrt   =                           (MarshalByRefType)   ad2.CreateInstanceAndUnwrap(         &#

16、160;                       exeAssembly,                                   typeof(MarshalByRefType).FullName            

17、            );                 /   Call   a   method   on   the   object   via   the   proxy,   passing   the                

18、   /   default   AppDomain 's   friendly   name   in   as   a   parameter.                 mbrt.SomeMethod(callingDomainName);                 /   Unload &#

19、160; the   second   AppDomain.   This   deletes   its   object   and                   /   invalidates   the   proxy   object.                 AppDoma

20、in.Unload(ad2);                 try                                          /   Call   the   method   again.   Note 

21、60; that   this   time   it   fails                           /   because   the   second   AppDomain   was   unloaded.                

22、        mbrt.SomeMethod(callingDomainName);                         Console.WriteLine( "Sucessful   call. ");                        &#

23、160;         catch(AppDomainUnloadedException)                                          Console.WriteLine( "Failed   call;   this   is   expected

24、. ");                            /   Because   this   class   is   derived   from   MarshalByRefObject,   a   proxy   /   to   a   MarshalByRefTy

25、pe   object   can   be   returned   across   an   AppDomain   /   boundary. public   class   MarshalByRefType   :   MarshalByRefObject          /     Call   this   method 

26、0; via   a   proxy.         public   void   SomeMethod(string   callingDomainName)                          /   Get   this   AppDomain 's   settings  

27、and   display   some   of   them.                 AppDomainSetup   ads   =   AppDomain.CurrentDomain.SetupInformation;                 Console.WriteLine( "AppName=0, 

28、0; AppBase=1,   ConfigFile=2 ",                           ads.ApplicationName,                           ads.ApplicationBase,      

29、;                     ads.ConfigurationFile                 );                 /   Display   the   name   of   the   calling  

30、; AppDomain   and   the   name                   /   of   the   second   domain.                 /   NOTE:   The   application 's   thread   has   transitioned   between                   /   AppDomains.  

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論