Type instances have different copies in different application domains
Source: Internet
Author: User
Today, let's look at CLR via C #. We can see that the same type will be applied in different applications. ProgramThere are multiple images in the domain, so I wrote a Section CodeVerify this situation. UsingSystem; UsingSystem. Collections. Generic; UsingSystem. LINQ; UsingSystem. text; UsingSystem. reflection;
NamespaceRemotingtest { ClassProgram { StaticVoidMain (String[] ARGs) {
Appdomain ad=Appdomain. createdomain ("Appdomain2");
Remotingobj Ro=Ad. createinstanceandunwrap (assembly. getexecutingassembly (). fullname,"Remotingtest. remotingobj")AsRemotingobj;//Ro is a local transparent proxy pointing to objects in the newly created application domain Console. writeline (Ro. indexof ("Michael Jackson","Jack"));//The remote object must have been created, but remotingobj. Flag is still 0. This indicates that the type object in the application domain is not set, and the typeof (remotingobj) in AD is set) BoolFlag=System. runtime. remoting. remotingservices. istransparentproxy (RO );
Console. writeline (FLAG );
Remotingobj RO2=NewRemotingobj ();//Objects in the application domain Flag=System. runtime. remoting. remotingservices. istransparentproxy (RO2 );
Console. writeline (FLAG ); IntA=Ro. typehashcode ();
A=Ro2.typehashcode ();//Through F10, we can see that hashcode changes indicate that typeof (remotingobj) represents different objects in different AppDomains. Console. Read ();
}
}
PublicClassRemotingobj: marshalbyrefobject { PublicRemotingobj () {< br> remotingobj. flag = 112 ;
console. writeline ( string . format ( " remotingobj ({0 }) is created in {1} ", This . gethashcode (), appdomain. currentdomain. friendlyname);
}
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