CreateInstanceAndUnwrap和域 [英] CreateInstanceAndUnwrap and Domain

查看:498
本文介绍了CreateInstanceAndUnwrap和域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我想在其他领域的实例的属性。

I have a property whose instance I want to be in other 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的)调用。但是,当我运行下面的代码,它仍然写入主应用程序域。

This works fine. But I assume all the method calls on _loader instance will invoke in other domain (myDomain). But when I run following code, it still writes main app domain.

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("-----------");
        }



是因为展开的()?我哪里做错了?

Is it because of Unwrap()? Where I am doing wrong?

我理解的AppDomain创建单独的内存。我需要的是我的主要应用程序运行时,它加载在不同的AppDomain模块。由于主要的应用程序还想看模块中的某些活动,并在单独的域名运行的对象interfare,什么是实现这一目标的最佳途径。

I understand AppDomain creates seperate memory. What I need is my main application runs, it loads modules in different AppDomain. Since main app also wants to watch some activity of modules and interfare with objects running in seperate domain, what is the best way to achieve it.

推荐答案

如果您想实际运行中的其他组件中的代码,你需要从的 ModuleLoader组件类继承:// MSDN。 microsoft.com/en-us/library/system.marshalbyrefobject.aspx> MarshalByRefObject的 。如果你这样做, CreateInstanceAndUnwrap()实际上将返回一个代理,呼叫将在其它应用程序域执行。

If you want to actually run the code in the other assembly, you need to make your ModuleLoader class inherit from MarshalByRefObject. If you do that, CreateInstanceAndUnwrap() will actually return a proxy and the call will be executed in the other appdomain.

如果你不这样做,而是将类标记为序列化(作为例外消息提示), CreateInstanceAndUnwrap()将在其他AppDomain中创建对象,序列化,序列化形式转移到原来的应用程序域,反序列化和有呼吁反序列化的实例的方法。

If you don't do that, and instead mark the class as Serializable (as an exception message suggests), CreateInstanceAndUnwrap() will create the object in the other appdomain, serialize it, transfer the serialized form to the original appdomain, deserialize it there and call the method on the deserialized instance.

这篇关于CreateInstanceAndUnwrap和域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆