标签:
11.1理解remoting
11.1.1应用程序域基本概念
.NET提供了一项技术,使得跨应用程序域中的对象也可以相互访问,该技术就是.NET remoting。(185)
11.1.2应用程序域的基本操作
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections; using System.IO; using System.Threading; using ClassLib; using System.Runtime.Remoting; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { ////获取当前代码所在的应用程序域 //AppDomain currentDomain = AppDomain.CurrentDomain; //currentDomain = Thread.GetDomain(); ////获取应用程序域名称 //string name = AppDomain.CurrentDomain.FriendlyName; ////创建新应用程序域 //AppDomain newDomain = AppDomain.CreateDomain("NewDomain"); ////在应用程序域中创建对象 //DemoClass obj = (DemoClass)AppDomain.CurrentDomain.CreateInstanceAndUnwrap("ClassLib", "ClassLib.DemoClass"); //ObjectHandle objHandle = AppDomain.CurrentDomain.CreateInstance("ClassLib", "ClassLib.DemoClass"); //obj = (DemoClass)objHandle.Unwrap(); ////判断是否为默认应用程序域 //bool isdefault = newDomain.IsDefaultAppDomain(); //string _path = AppDomain.CurrentDomain.SetupInformation.ApplicationBase; //Test1(); Test2(); Console.Read(); } static void Test1() { AppDomain currentDomain = AppDomain.CurrentDomain; Console.WriteLine(currentDomain.FriendlyName); DemoClass obj; obj = (DemoClass)currentDomain.CreateInstanceAndUnwrap("ClassLib", "ClassLib.DemoClass"); obj.ShowAppDomain(); obj.ShowCount("Jimmy"); obj.ShowCount("Jimmy"); } static void Test2() { AppDomain currentDomain = AppDomain.CurrentDomain; Console.WriteLine(currentDomain.FriendlyName); AppDomain newDomain = AppDomain.CreateDomain("NewDomain"); DemoClass obj, obj2; obj = (DemoClass)newDomain.CreateInstanceAndUnwrap("ClassLib", "ClassLib.DemoClass"); ; obj.ShowAppDomain(); obj.ShowCount("Jimmy"); obj.ShowCount("Jimmy"); obj2 = (DemoClass)newDomain.CreateInstanceAndUnwrap("ClassLib", "ClassLib.DemoClass"); ; obj2.ShowAppDomain(); obj2.ShowCount("Jimmy"); obj2.ShowCount("Jimmy"); } } }
标签:
原文地址:http://www.cnblogs.com/liuslayer/p/5607959.html