using System; using System.Reflection; using System.Threading; namespace TestTwoDomainsAndSingeThread { public class Worker : MarshalByRefObject { public void PrintDomain() { Console.WriteLine("Object is executing in AppDomain \"{0}\"", AppDomain.CurrentDomain.FriendlyName); Console.WriteLine("AppDomain for current thread: " + Thread.GetDomain().FriendlyName); Console.WriteLine("Current thread ID: " + Thread.CurrentThread.ManagedThreadId.ToString()); } } public class Test { static void Main(string[] args) { // Create an ordinary instance in the current AppDomain Worker localWorker = new Worker(); localWorker.PrintDomain(); // Create a new application domain, create an instance // of Worker in the application domain, and execute code // there. AppDomain ad = AppDomain.CreateDomain("New domain"); Worker remoteWorker = (Worker)ad.CreateInstanceAndUnwrap( typeof(Worker).Assembly.FullName, typeof(Worker).FullName); remoteWorker.PrintDomain(); Console.ReadLine(); } } }