如果跨应用程序域访问类中的静态数据,该怎么办? [英] What happens to the static data in a class if it is accessed across app domains?
问题描述
我有一个静态类,其中包含一些静态数据。如果从不同的应用程序域访问数据,该怎么办?
I have a static class which has some static data. What happens to the data if its accessed from different app domain?
-
每个域都会有一个静态类的副本吗?
Will there a copy of a static class for each domain?
将复制原始类型吗?
如果数据可序列化怎么办?
What if the data is serializable?
推荐答案
AppDomain之间的内存不共享。默认情况下,对象是深层克隆,如果它们是MarshalByRef,则其类似于在AppDomain上执行调用的远程处理,因此看起来是其共享状态。
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
是通过交换跨应用程序域边界进行通信的对象的基类使用代理的邮件。不继承自MarshalByRefObject
的对象按值隐式封送。当远程应用程序引用按值编组对象时,该对象的副本将跨应用程序域边界传递。
MarshalByRefObject
is the base class for objects that communicate across application domain boundaries by exchanging messages using a proxy. Objects that do not inherit fromMarshalByRefObject
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.
我不相信您实际上可以使用AppDomain方法调用静态成员,最好的选择是将静态调用包装在实例类中并使用 DoCallback
以便在另一个域中执行该代码并在 MarshalByRef
对象。
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.
请参见 MSDN
这篇关于如果跨应用程序域访问类中的静态数据,该怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!