【发布时间】:2013-05-11 02:16:10
【问题描述】:
请考虑以下代码:
// Create a new application domain
AppDomain ad = AppDomain.CreateDomain("New domain");
Worker work = new Worker();
// if Worker class is marked as 'MarshalByRefObject', this will run in current
// appdomain.
// if Worker class is NOT marked as 'MarshalByRefObject' and is marked as
// 'Serializable', this will run in a new appdomain.
ad.DoCallBack(work.PrintDomain);
// or ad.DoCallBack(new CrossAppDomainDelegate(work.PrintDomain));
// But for static methods:
// If ppp method is static, no marking is required and it will run in
// a new AppDomain.
ad.DoCallBack(Worker.ppp);
我们如何解释DoCallBack的这种行为?
- 当
Worker类被标记为MarshalByRefObject时,为什么非静态方法PrintDomain在当前域中执行? - 当
Worker类标记为Serializable时,为什么非静态方法PrintDomain在新的AppDomain 中执行? - 为什么静态方法不需要任何标记?
【问题讨论】:
标签: c#-4.0 .net-4.0 appdomain applicationdomain