6
請考慮下面的代碼:DoCallBack CrossAppDomainDelegate行爲非靜態代表
// 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
這種行爲?
- 爲什麼非靜態方法
PrintDomain
在當前域中執行時Worker
類被標記MarshalByRefObject
? - 爲什麼在
Worker
類標記爲Serializable
時,在新的AppDomain中執行非靜態方法PrintDomain
? - 爲什麼靜態方法不需要任何標記?
您的方法'PrintDomainStatic'不是靜態的。當使用代理('MarshalByRefObject'取消註釋)輸出是'ConsoleApplication1.vshost.exe測試' – Troopers