CreateInstanceAndUnwrap和Domain

时间:2011-08-12 18:54:16

标签: c# plugins appdomain

我有一个属性,我想要在其他域中使用它。

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中加载模块。由于主应用程序还希望观看模块的一些活动以及在单独域中运行的对象,因此实现它的最佳方法是什么。

1 个答案:

答案 0 :(得分:33)

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

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

相关问题