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. Program There are multiple images in the domain, so I wrote a Section Code Verify this situation. Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;
Using System. reflection;

Namespace Remotingtest
{
Class Program
{
Static   Void Main ( String [] ARGs)
{
Appdomain ad = Appdomain. createdomain ( " Appdomain2 " );
Remotingobj Ro = Ad. createinstanceandunwrap (assembly. getexecutingassembly (). fullname, " Remotingtest. remotingobj " ) As Remotingobj; // 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)
Bool Flag = System. runtime. remoting. remotingservices. istransparentproxy (RO );
Console. writeline (FLAG );
Remotingobj RO2 =   New Remotingobj (); // Objects in the application domain
Flag = System. runtime. remoting. remotingservices. istransparentproxy (RO2 );
Console. writeline (FLAG );
Int A = 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 ();
}
}

Public   Class Remotingobj: marshalbyrefobject
{
Public Remotingobj ()
{< br> remotingobj. flag = 112 ;
console. writeline ( string . format ( " remotingobj ({0 }) is created in {1} " , This . gethashcode (), appdomain. currentdomain. friendlyname);
}

Public   Int Indexof ( String Str1, String Str2)
{
ReturnStr1.indexof (str2 );
}

Public   Int Typehashcode ()
{
Return Typeof(Remotingobj). gethashcode ();
}

Public   Static   Int Flag;
}
}

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.