Bitte betrachten Sie das folgende Stück Code:DoCallBack CrossAppDomainDelegate Verhalten für nicht-statisch Delegierten
// 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);
Wie erklären wir das Verhalten von DoCallBack
?
- Warum ist die nicht-statische Methode
PrintDomain
in der aktuellen Domäne ausgeführt, wenn dieWorker
KlasseMarshalByRefObject
markiert? - Warum wird die nicht statische Methode
PrintDomain
in einer neuen AppDomain ausgeführt, wenn dieWorker
-Klasse mitSerializable
markiert ist? - Warum benötigt die statische Methode keine Markierungen?
Ihre Methode 'PrintDomainStatic' ist nicht statisch. Wenn der Proxy verwendet wird ('MarshalByRefObject' unkommentiert), lautet die Ausgabe' ConsoleApplication1.vshost.exe Test' – Troopers