如果跨应用程序域访问类中的静态数据会怎样? [英] What happens to the static data in a class if it is accessed across app domains?

查看:28
本文介绍了如果跨应用程序域访问类中的静态数据会怎样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个静态类,其中包含一些静态数据.如果从不同的应用程序域访问数据,会发生什么情况?

  1. 每个域都会有一个静态类的副本吗?

  2. 原始类型会被复制吗?

  3. 如果数据是可序列化的怎么办?

解决方案

AppDomain 之间的内存不共享.默认情况下,对象是深度克隆,如果它们是 MarshalByRef,那么它类似于远程处理,其中跨 AppDomain 执行调用,因此它看起来是共享状态.

<块引用>

MarshalByRefObject 是通过使用代理交换消息而跨应用程序域边界进行通信的对象的基类.不从 MarshalByRefObject 继承的对象隐式按值编组.当远程应用程序通过值对象引用编组时,该对象的副本将跨应用程序域边界传递.

我不相信您实际上可以使用 AppDomain 方法调用静态成员,最好的办法是将静态调用包装在实例类中并使用 DoCallback 在另一个域中执行该代码并在 MarshalByRef 中收集状态 对象.

查看 MSDN

I have a static class which has some static data. What happens to the data if its accessed from different app domain?

  1. Will there a copy of a static class for each domain?

  2. Will the primitive types be copied?

  3. What if the data is serializable?

解决方案

The memory between AppDomain's is not shared. By default the objects are a deep clone, if they are MarshalByRef then its similar to remoting where the calls are executed across AppDomain, so it appears that its shared state.

MarshalByRefObject is the base class for objects that communicate across application domain boundaries by exchanging messages using a proxy. Objects that do not inherit from MarshalByRefObject are implicitly marshal by value. When a remote application references a marshal by value object, a copy of the object is passed across application domain boundaries.

I don't believe you can actually invoke static members using the AppDomain methods, your best bet would be to wrap the static calls in an instance class and use DoCallback to execute that code in the other domain and collect the state in a MarshalByRef object.

See the example on MSDN

这篇关于如果跨应用程序域访问类中的静态数据会怎样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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