Explanation of the System. AppDomain class (2). Explanation of the C system Function

Source: Internet
Author: User

Explanation of the System. AppDomain class (2). Explanation of the C system Function

Processes have independent memory and resources, but AppDomain is only a logical abstraction. A process can have multiple AppDomains. Data in each AppDomain is independent of each other. A thread can travel through multiple AppDomains.

I. Attributes

ActivationContext gets the activation context of the current application domain.
ApplicationIdentity obtains the Application ID in the application domain.
ApplicationTrust indicates the permission granted to the application and whether the application has the trust level that allows it to run.
BaseDirectory gets the base Directory, which is used by the Assembly conflict resolution program to detect the assembly.
CurrentDomain gets the current application domain of the current Thread.
DomainManager obtains the Domain manager provided by the host when initializing the application domain.
DynamicDirectory gets the Directory, which is used by the Assembly conflict resolution program to detect dynamically created assembly.
Evidence obtains the Evidence associated with the application domain.
FriendlyName gets the friendly name of this application domain.
Id to obtain an integer that uniquely identifies the application domain in the process.
IsFullyTrusted gets a value that indicates whether the Assembly loaded to the current application domain is executed in full trust mode.
IsHomogenous gets a value that indicates whether the current application domain has a permission set granted to all the Assemblies loaded into the application domain.
MonitoringIsEnabled gets or sets a value indicating whether to enable CPU and memory monitoring of the application domain for the current process. Once monitoring is enabled for a process, it cannot be disabled.
Monitoring1_vedmemorysize is used to obtain the number of bytes that are retained after the collection is completely blocked and referenced by the current application domain.
Monitoring1_vedprocessmemorysize gets the total number of bytes retained after the last full blocking of all application domains in the process.
MonitoringTotalAllocatedMemorySize obtains the total size of all memory allocated by the application domain since the application domain is created (in bytes, no memory collected is deducted ).
MonitoringTotalProcessorTime gets the total processor time used by all threads to execute in the current application domain since the process is started.
PermissionSet obtains the permission set of the sandbox application domain.
RelativeSearchPath gets the path under the base Directory, in which the Assembly conflict resolution program should detect the dedicated assembly.
SetupInformation: obtains the application domain configuration information of this instance.
ShadowCopyFiles indicates whether the application domain is configured as an image copy file.

1 public class Program 2 {3 static void Main (string [] args) 4 {5 AppDomain appdomain = AppDomain. CurrentDomain; // obtain the current application domain of the current Thread. 6 Console. WriteLine (appdomain. Id); // output 1 to obtain an integer that uniquely identifies the application domain in the process. 7 Console. WriteLine (appdomain. ActivationContext); // blank output to obtain the activation context of the current application domain. 8 Console. WriteLine (appdomain. ApplicationIdentity); // blank output to obtain the application ID in the application domain. 9 10 ApplicationTrust AT = appdomain. ApplicationTrust; // obtain information about the permissions granted to the application and whether the application has a trust level that allows it to run. 11 Console. writeLine (appdomain. applicationTrust); // output System. security. policy. applicationTrust 12 13 Console. writeLine (appdomain. baseDirectory); // Output F: \ xxx \ ConsoleApplication1 \ bin \ Debug \ to obtain the base Directory, which is used by the Assembly conflict resolution program to detect the assembly. 14 15 AppDomainManager Adm = appdomain. DomainManager; // obtain the Domain manager provided by the host when initializing the application domain. 16 Console. writeLine (Adm. entryAssembly. location); // Output F: \ xxx \ ConsoleApplication1 \ bin \ Debug \ ConsoleApplication1.exe17 Console. writeLine (appdomain. domainManager); // output Microsoft. visual Studio. hostingProcess. VSHostAppDomainManager18 19 Console. writeLine (appdomain. dynamicDirectory); // outputs a blank Directory, which is used by the Assembly conflict resolution program to detect dynamically created assembly. 20 21 Evidence ed = appdomain. Evidence; // another item obtains the Evidence associated with the application domain. 22 Console. writeLine (appdomain. evidence. toString (); // output System. security. policy. evidence23 24 Console. writeLine (appdomain. friendlyName); // output leleapplication1.vshost.exe to get the friendly name of this application domain. 25 26 Console. WriteLine (appdomain. IsFullyTrusted); // outputs True to obtain a value indicating whether the Assembly loaded to the current application domain is fully trusted. 27 28 Console. writeLine (appdomain. isHomogenous); // outputs True to obtain a value that indicates whether the current application domain has a permission set granted to all the Assemblies loaded into the application domain. 29 30 Console. WriteLine (AppDomain. MonitoringIsEnabled); // outputs False to obtain or set a value indicating whether to enable CPU and memory monitoring of the application domain for the current process. Once monitoring is enabled for a process, it cannot be disabled. 31 32 AppDomain. monitoringIsEnabled = true; 33 // This attribute can be used with 34 Console only after it is enabled. writeLine (appdomain. monitoring1_vedmemorysize); // the output value 0 indicates the number of bytes that are retained after the collection is completely blocked and referenced by the current application domain. 35 Console. writeLine (appdomain. monitoringTotalAllocatedMemorySize); // output 0 gets the total size of all memory allocated by the application domain since the application domain is created (in bytes, no memory collected ). 36 Console. WriteLine (appdomain. MonitoringTotalProcessorTime); // output 00:00:00 get the total processor time used by all threads to execute in the current application domain since the process starts. 37 38 System. Security. PermissionSet ps = appdomain. PermissionSet; // obtain the permission set of the sandbox application domain. 39 40 Console. writeLine (appdomain. permissionSet. toString (); // output <PermissionSet class = "System. security. permissionSet "version =" 1 "Unrestricted =" true "/> 41 42 Console. writeLine (appdomain. relativeSearchPath); // output blank 43 44 Console. writeLine (appdomain. setupInformation); // output AppDomainSetup to obtain the application domain configuration information of this instance. 45 46 Console. writeLine (appdomain. shadowCopyFiles); // output False47 48 File. writeAllText (@ "D: \ 123.txt", appdomain. permissionSet. toString (); 49 50 // SetupInformation gets the application domain configuration information for this instance. 51 // ShadowCopyFiles indicates whether the application domain is configured as an image copy file. 52 53 Console. ReadKey (); 54}

Ii. Method

Description
ApplyPolicy returns the display name of the Assembly after the application policy.
CreateComInstanceFrom (String, String) creates a new instance of the specified COM type. The name of the file specified by the parameter. The file contains the Assembly containing the type and type name.
CreateDomain (String) creates an application domain with the specified name.
CreateInstance (String, String) creates a new instance of the specified type defined in the specified dataset.
CreateInstanceAndUnwrap (String, String) creates a new instance of the specified type. The parameter specifies the assembly of the definition type and the name of the type.
CreateInstanceFrom (String, String) creates a new instance of the specified type defined in the specified Assembly file.
CreateObjRef creates an object that contains all the information required to generate a proxy for communication with a remote object. (Inherited from MarshalByRefObject .)
DefineDynamicAssembly (AssemblyName, AssemblyBuilderAccess) defines a dynamic assembly with a specified name and access mode.
DoCallBack executes code in another application domain, which is identified by the specified delegate.
ExecuteAssembly (String) executes the Assembly contained in the specified file.
ExecuteAssemblyByName (String) executes an assembly given its display name.
GetAssemblies gets the assembly in the execution context loaded to this application domain.
GetData gets the value stored in the current application domain with the specified name.
GetLifetimeService searches the service objects that control the current lifetime of the Instance's lifetime policy. (Inherited from MarshalByRefObject .)
InitializeLifetimeService grants the AppDomain unlimited lifetime by preventing lease creation. (Rewrite externalbyrefobject. InitializeLifetimeService ().)
IsCompatibilitySwitchSet gets a Boolean value that can be null. This value indicates whether any compatibility switch is set. If yes, it indicates whether the specified compatibility switch is set.
Isdefaappappdomain returns a value indicating whether the application domain is the default application domain of the process.
IsFinalizingForUnload indicates whether the application domain is being detached and whether the objects contained in the domain are being terminated when the public language is running.
Load (AssemblyName) loads an Assembly when an AssemblyName is specified.
ReflectionOnlyGetAssemblies returns the only reflection context assembly that has been loaded into the application domain.
SetData (String, Object) assigns a specified value for the specified application domain attribute.
SetData (String, Object, IPermission) assigns the specified value to the specified application domain attribute. When retrieving this attribute, the caller must have the specified permission.
SetPrincipalPolicy specifies how the user and identity object should be appended to the thread if the thread tries to bind to the user during execution in this application domain.
SetThreadPrincipal is set to be appended to the default subject object of the thread in the following cases, that is, if the thread tries to bind to the subject when the thread is executed in this application domain.
Unload: Unload the specified application domain.

1 class Program 2 {3 static void Main (string [] args) 4 {5 AppDomain app = AppDomain. createDomain ("test program domain"); // use the specified name to create the application domain 6 ObjectHandle objHan = app. createInstance ("MySpace", "MySpace. person "); // create the object specified in the specified Assembly 7 var obj = objHan. unwrap (); 8 Console. writeLine (obj. toString (); // output MySpace. person 9 10 object obj2 = app. createInstanceAndUnwrap ("MySpace", "MySpace. person "); // create the specified object in the specified program. The Unwr Ap method 11 Console. writeLine (obj2.ToString (); // output MySpace. person12 13 ObjectHandle objHan2 = app. createInstanceFrom (@ "D: \ MySpace. dll "," MySpace. person "); // create class object from the specified Assembly 14 object obj3 = objHan2.Unwrap (); 15 Console. writeLine (obj3.ToString (); // output MySpace. person16 17 // app. createObjRef (); 18 19 app. executeAssembly (@ "D: \ ConsoleApplication1.exe"); // execute the Assembly output 012345678920 contained in the specified file // D: \ ConsoleApplication1. The exe code is as follows: 21 // static void Main (string [] args) 22 // {23 // for (int I = 0; I <10; I ++) 24 // {25 // Console. writeLine (I); 26 //} 27 28 // Console. readKey (); 29 //} 30 31 // change D: \ ConsoleApplication1.exe to create a file under drive D and write a text 32 app. executeAssembly (@ "D: \ ConsoleApplication1.exe"); // after running, a file is created under drive D and the text 33 // static void Main (string [] args) is written) 34 // {35 // File. writeAllText (@ "D: \ AppDomainTest.txt", "test"); 36/ /Console. readKey (); 37 //} 38 // note that the Console is available this time. readKey (); therefore, You need to press the keyboard twice to complete the application domain execution before returning to the main program domain 39 Assembly [] assArr = app. getAssemblies (); // obtain all the Assemblies loaded to the app domain 40 41 foreach (var ass in assArr) 42 {43 Console. writeLine (ass. fullName); // such as leleapplication1, Version = 1.0.0.0, Culture = neutral, PublicKeyToken = null44} 45 46 // The Assembly 47 app is loaded for a given Assembly. load ("MySpace"); 48 Console. writeLine (app. isDefault AppDomain (); // outputs False to indicate whether the default application domain of the process is 49 50 apps. setData ("data1", ""); // assign a specified value to the specified application domain attribute. 51 object o = app. getData ("data1"); 52 Console. writeLine (o. toString (); // output the display name of the Assembly after the application policy is returned by 53 54 // ApplyPolicy. 55 // CreateComInstanceFrom (String, String) create a new instance of the specified COM type. The name of the file specified by the parameter. The file contains the Assembly containing the type and type name. 56 // CreateObjRef creates an object that contains all the information required to generate a proxy for communication with a remote object. (Inherited from MarshalByRefObject .) 57 // DefineDynamicAssembly (AssemblyName, AssemblyBuilderAccess) defines a dynamic assembly with the specified name and access mode. 58 // DoCallBack executes code in another application domain, which is identified by the specified delegate. 59 // ExecuteAssemblyByName (String) executes the Assembly given its display name. 60 // GetLifetimeService retrieval the current lifetime service object that controls the life cycle policy of this instance. (Inherited from MarshalByRefObject .) 61 // InitializeLifetimeService grants AppDomain unlimited lifetime by preventing lease creation. (Rewrite externalbyrefobject. InitializeLifetimeService ().) 62 // IsCompatibilitySwitchSet obtains a Boolean value that can be null. This value indicates whether any compatibility switch is set. If yes, it indicates whether the specified compatibility switch is set. 63 // IsFinalizingForUnload indicates whether the application domain is being detached and whether the objects contained in the domain are terminated when the common language is running. 64 // ReflectionOnlyGetAssemblies returns the only reflection context assembly that has been loaded to the application domain. 65 // SetPrincipalPolicy specifies how the user and identity object should be appended to the thread if the thread tries to bind to the user during execution in this application domain. 66 // SetThreadPrincipal is set to be appended to the default subject object of the thread in the following cases, that is, if the thread tries to bind to the subject when the thread is executed in this application domain. 67 // Unload the specified application domain. 68 69 Console. ReadKey (); 70} 71}

Http://www.cnblogs.com/kissdodog/archive/2013/05/09/3069553.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.