CreateInstanceAndUnwrap和Domain

我有一个属性,其实例我想在其他域中。

public ModuleLoader Loader { get { if(_loader == null) _loader = (ModuleLoader)myDomain.CreateInstanceAndUnwrap( this.GetType().Assembly.FullName, "ModuleLoader", false, System.Reflection.BindingFlags.CreateInstance, null, null, null, null); System.Diagnostics.Debug.WriteLine("Is proxy={0}", RemotingServices.IsTransparentProxy(_loader)); //writes false _loader.Session = this; return _loader; } } 

这很好用。 但我假设_loader实例上的所有方法调用都将在其他域(myDomain)中调用。 但是当我运行以下代码时,它仍然会编写主应用程序域。

 public void LoadModule(string moduleAssembly) { System.Diagnostics.Debug.WriteLine("Is proxy={0}", RemotingServices.IsTransparentProxy(this)); System.Diagnostics.Debug.WriteLine( AppDomain.CurrentDomain.FriendlyName); System.Diagnostics.Debug.WriteLine("-----------"); } 

是因为Unwrap()? 我在哪里做错了?

我知道AppDomain会创建单独的内存。 我需要的是我的主应用程序运行,它在不同的AppDomain中加载模块。 由于主应用程序还希望观看模块的一些活动以及与在单独域中运行的对象进行交互,因此实现它的最佳方法是什么。

如果要在其他程序集中实际运行代码,则需要使ModuleLoader类inheritance自MarshalByRefObject 。 如果这样做, CreateInstanceAndUnwrap()将实际返回一个代理,并且该调用将在另一个appdomain中执行。

如果你不这样做,而是将类标记为Serializable (作为exception消息建议), CreateInstanceAndUnwrap()将在其他appdomain中创建对象,序列化它,将序列化表单传输到原始appdomain,在那里反序列化并在反序列化的实例上调用该方法。