新知識點:跨域
運行示例:dom
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Reflection; using System.Runtime.Remoting; namespace FactoryMode { [Serializable] public class Program : MarshalByRefObject { //在調用此方法時,觀察"調用堆棧",發現一次"外部代碼"的切換,代表自進行了一次跨域操做 public void NotMainMethod() { //AppDomain.CurrentDomain 能夠獲得一樣的結果 var curCallingDomain = Thread.GetDomain(); var curCallingDomainName = curCallingDomain.FriendlyName; Console.WriteLine("Cross-domain calling, curdomain name={0}.", curCallingDomainName); } static void Main(string[] args) { var curCallingDomain = Thread.GetDomain(); var curCallingDomainName = curCallingDomain.FriendlyName; Console.WriteLine("Default AppDomain's friendly name={0}.", curCallingDomainName); string exeAssembly = Assembly.GetExecutingAssembly().CodeBase; Console.WriteLine("Main assembly={0}.", exeAssembly); AppDomain otherDomain = null; Console.WriteLine("{0}Demo #1", Environment.NewLine); otherDomain = AppDomain.CreateDomain("AD #2", null, null); //在"AD#2"應用程序域中建立 Programs 對象,並向默認應用程序域返回它的引用; var mbrt = (Program)otherDomain.CreateInstanceFromAndUnwrap(exeAssembly, "FactoryMode.Program"); Console.WriteLine("Type={0}", mbrt.GetType()); Console.WriteLine("Is proxy={0}", RemotingServices.IsTransparentProxy(mbrt)); //利用引用調用方法,方法中的打印顯示出此時線程正運行在"AD#2"應用程序域中 mbrt.NotMainMethod(); Console.Read(); } } }
備註:性能
卸載AppDomain:spa
監視AppDomain:線程
很是好用的功能,能夠用來作資源監控,性能監測;code