请考虑以下代码片段:
// 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中执行? - 为什么静态方法不需要任何标记?
PrintDomainStatic
不是静态的。当代理被使用时(取消注释MarshalByRefObject
),输出为ConsoleApplication1.vshost.exe Test
。 - Troopers