/***appdomain represents an application domain, which is an isolated environment in which an application executes. This class cannot be inherited. application domains (represented by AppDomain objects) provide isolation, offload, and security boundaries for executing managed code. Use application domain isolation to potentially terminate a process's tasks. If the state of the AppDomain that is performing the task becomes unstable, you can uninstall the AppDomain without affecting the process. This is important when the process must run for a long time without restarting. You can also use application domains to isolate tasks that should not share data. If the assembly is loaded into the default application domain, the assembly cannot be unloaded from memory when the process is running. However, if you open another application domain to load and execute the assembly, uninstalling the application domain also uninstalls the assembly. Use this technique to minimize the working set of long-running processes that occasionally use large DLLs. Multiple application domains can run in a process, but there is no one-to-many association between application domains and threads. Multiple threads can belong to an application domain, although the given thread is not limited to an application domain, but at any given time, the thread executes in an application domain. The application domain is created by using the CreateDomain method. The AppDomain instance is used to load and execute assemblies (Assembly). When you no longer use the AppDomain, you can uninstall it. The AppDomain class implements a set of events that enable an application to respond when loading an assembly, unloading an application domain, or throwing an unhandled exception. For more information about using application domains, see application domains. This class implements the MarshalByRefObject, _appdomain, and Ievidencefactory interfaces. In any case, you should not create a remote-controlled wrapper for the AppDomain object. Doing so publishes a remote reference to the AppDomain, exposes such as the CreateInstance method to remote access, and effectively damages the AppDomain's Code access security. A malicious client connected to a remote AppDomain can gain access to all the resources that the AppDomain itself can access. You should not create a remotely controlled wrapper for any of the following types: the type of the extension MarshalByRefObject and the type of method that implements the methods that the malicious client can use to bypass the security system. **///Warning//the default value of the Appdomainsetup.disallowcodedownload property is false. This setting is not secure for services. To prevent the service from downloading partially trusted code, set this property to True. //Example//This example shows how to create a new AppDomain, instantiate a type in the new AppDomain, and communicate with objects of that type. //In addition, this example shows how to unload an AppDomain that causes an object to be garbage collected. usingSystem;usingSystem.Reflection;usingSystem.Threading;classmodule1{ Public Static voidMain () {//Get and display the friendly name of the default AppDomain. stringCallingdomainname =Thread.getdomain (). FriendlyName; Console.WriteLine (Callingdomainname); //Get and display the full name of the EXE assembly. stringexeassembly =assembly.getentryassembly (). FullName; Console.WriteLine (exeassembly); //Construct and initialize settings for a second AppDomain.AppDomainSetup ads =NewAppDomainSetup (); Ads. ApplicationBase=System.Environment.CurrentDirectory; Ads. Disallowbindingredirects=false; Ads. Disallowcodedownload=true; Ads. ConfigurationFile=AppDomain.CurrentDomain.SetupInformation.ConfigurationFile; //Create the second AppDomain.AppDomain ad2 = Appdomain.createdomain ("AD #2",NULL, ADS); //Create A instance of Marshalbyreftype in the second AppDomain. //A Proxy to the object is returned.Marshalbyreftype MBRT =(Marshalbyreftype) ad2. Createinstanceandunwrap (exeassembly,typeof(Marshalbyreftype). FullName); //Call a method on the object via the proxy, passing the//default AppDomain ' s friendly name in as a parameter.MBRT. SomeMethod (Callingdomainname); //Unload the second AppDomain. This deletes its object and//invalidates the proxy object.appdomain.unload (AD2); Try { //Call the method again. Note that this time it fails//because the second AppDomain was unloaded.MBRT. SomeMethod (Callingdomainname); Console.WriteLine ("sucessful call."); } Catch(appdomainunloadedexception) {Console.WriteLine ("Failed call; expected."); } }}//Because This class was derived from MarshalByRefObject, a proxy//To a Marshalbyreftype object can is returned across an AppDomain//boundary. Public classmarshalbyreftype:marshalbyrefobject{//Call the This method via a proxy. Public voidSomeMethod (stringcallingdomainname) { //Get This AppDomain ' s settings and display some of them.AppDomainSetup ads =AppDomain.CurrentDomain.SetupInformation; Console.WriteLine ("appname={0}, Appbase={1}, configfile={2}", ads. ApplicationName, ads. ApplicationBase, ads. ConfigurationFile); //Display The name of the calling AppDomain and the name//Of the second domain. //note:the application ' s thread has transitioned between//AppDomains.Console.WriteLine ("calling from ' {0} ' to ' {1} '.", Callingdomainname, Thread.getdomain (). FriendlyName); }}
C # AppDomain Class