C# AppDomain_第1页
C# AppDomain_第2页
C# AppDomain_第3页
C# AppDomain_第4页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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

提交评论