【IT168技术文档】
Application Domain可以看作是一个Assembly的逻辑容器。在程序执行过程中,如果遇到需要的Type并没有定义在已经加载的Assemblies中, CLR会把相应的Assembly加载的该Application Domain中。每个Application Domain都有一个属于自己的加载器堆(Loader Heap),用于维护从Application Domain创建以后所用到的所有的Type,以及这些Type对应的方法表——维护这样一个Mapping:定义在Type中的所有方法和经过JIT编译后x86代码(只考虑32bit处理器)。
Application Domain之间是相互隔离,互不干扰。在一个Application Domain创建的对象不能被另一个Application Domain直接调用,反映在内存分配上面——就是各个Application Domain使用各个独立的内存地址空间。一个对象根据他所对应的类型(如System.MarshalByRefObject通过传递引用的方式)或者属性(比如对于定义了System. SerializableAttribute的Type采用传递值得方式)以两种不同的方式在Application Domain之间传递——By Reference 和By Value。
这些都是地球人都知道的.NET的基本原理,但是相信很多人没有尝试过通过Coding的方式证明这种机制。
那么现在我们就先来看看我们的Sample:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting;
using System.Reflection;
using Artech.AppDomainIsolation;
namespace Artech.AppDomainIsolation
{
public class GeneralType
{
AppDomain GetAppDomain()
{
return AppDomain.CurrentDomain;
}
}
[Serializable]
public class MarshalByValueType
{
public AppDomain GetAppDomain()
{
return AppDomain.CurrentDomain;
}
}
public class MarshalByRefType : MarshalByRefObject
{
public AppDomain GetAppDomain()
{
return AppDomain.CurrentDomain;
}
}
class Program
{
static void Main(string[] args)
{
AppDomain appDomain = AppDomain.CreateDomain("newAppDomain");
string assemblyName = "Artech.AppDomainIsolation";
try
{
GeneralType generalObject = (GeneralType)appDomain.CreateInstanceAndUnwrap(assemblyName, "Artech.AppDomainIsolation.GeneralType");
}
catch (Exception ex)
{
Console.WriteLine("Fail to pass a general type instance created in another application domain to default appliction domain,beacuse \"{0}\"", ex.Message);
}
MarshalByValueType marshalByValueObject = (MarshalByValueType)appDomain.CreateInstanceAndUnwrap(assemblyName, "Artech.AppDomainIsolation.MarshalByValueType");
MarshalByRefType marshalByRefObject = (MarshalByRefType)appDomain.CreateInstanceAndUnwrap(assemblyName, "Artech.AppDomainIsolation.MarshalByRefType");
Console.WriteLine("\nmarshalByValueObject is a transparent proxy? {0}.",RemotingServices.IsTransparentProxy(marshalByValueObject)?"Yes":"No");
Console.WriteLine("marshalByRefObject is a transparent proxy? {0}.", RemotingServices.IsTransparentProxy(marshalByRefObject) ? "Yes" : "No");
AppDomain appDomain_MarshalByValueType = marshalByValueObject.GetAppDomain();
AppDomain appDomain_MarshalByRefType = marshalByRefObject.GetAppDomain();
Console.WriteLine("\nThe method of marshalByValueObject is executed in the default application domain?\"{0}\"",
object.ReferenceEquals(AppDomain.CurrentDomain, appDomain_MarshalByValueType) ? "Yes" : "No");
Console.WriteLine("The method of marshalByRefObject is executed in the default application domain?\"{0}\"",
object.ReferenceEquals(AppDomain.CurrentDomain, appDomain_MarshalByRefType) ? "Yes" : "No");
AppDomain.Unload(appDomain_MarshalByRefType);
marshalByValueObject.GetAppDomain();
try
{
marshalByRefObject.GetAppDomain();
}
catch (Exception ex)
{
Console.WriteLine("\nAn exception is thorwn when calling the method of marshalByRefObject because \"{0}\"", ex.Message);
}
Console.Read();
}
}
}