Yes, Unwrap will load the assembly into the main domain too. Therefore, it is done correctly in another way:
- a descendant class MarshalByRefObject is created in the main, trusted assembly;
- an instance of this class is loaded in the new domain, and Unwrap is done to it (the trusted instance);
- all further interaction with objects in the new domain is done through this object.
For the situation of plug-ins, it makes sense for each interface that can be implemented by the plug-in to prepare your cross-domain proxy for loading in the plug-in domain. In the simplest case, such a proxy will simply delegate all calls:
class FooCrossDomainProxy: MarshalByRefObject, IFoo { private readonly IFoo target; public void FooCrossDomainProxy(IFoo target) { this.target = target; } public void Bar() => target.Bar(); }
In a more complicated case, this proxy can be assigned to adapt the interface to cross-domain interaction. For example, since calls between domains are slow, it makes sense to reduce their number by combining methods:
class FooCrossDomainProxy: MarshalByRefObject { private readonly IFoo target; public void FooCrossDomainProxy(IFoo target) { this.target = target; } public bool TryBar() { if (!target.CanBar) return false; target.Bar(); return true; } }
Unwrap? Cross-border application domains should not transfer objects defined in partially trusted assemblies. For good, partially trusted assemblies should be in a separate path that is not included in the list of paths to search for assemblies for the main domain. - PetSerAlbool CanProcess(object)andvoid Process(object). You want to load this plugin into a separate AppDomain with restrictions, and call these methods, sure that it will not cause an unexpected deletion of data from the file system. Probably there is a wrong design of the system. - Lunar Whisper