Hãy xem xét các đoạn mã sau:DoCallBack CrossAppDomainDelegate hành vi cho các đại biểu không tĩnh
// 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);
Làm thế nào để chúng tôi giải thích hành vi này của DoCallBack
?
- Tại sao phương pháp không tĩnh
PrintDomain
thực hiện trong lĩnh vực hiện nay khi lớpWorker
được đánh dấuMarshalByRefObject
? - Tại sao phương pháp không tĩnh
PrintDomain
được thực hiện trong một AppDomain mới khi lớpWorker
được đánh dấuSerializable
? - Tại sao phương pháp tĩnh không cần bất kỳ dấu hiệu nào?
Phương thức của bạn 'PrintDomainStatic' không tĩnh. Khi proxy được sử dụng ('MarshalByRefObject' uncommented) đầu ra là' ConsoleApplication1.vshost.exe Test' – Troopers