具有 MarshalByRefObject 字段的可序列化对象
好吧,我不确定这个问题以前是否被问过,所以它是否已经消失了。 假设我们有两个这样的类,
[Serializable]
public class ClassA
{
private string _name;
private ClassB _data;
}
public class ClassB : MarshalByRefObject
{
public string GetAppDomainName()
{
return AppDomain.Current.FriendlyName;
}
}
如您所见,ClassA 持有对 ClassB 的引用,但类 B 继承自 MarshalByRefObject 类。 我的问题是,当我尝试将 ClassA 传递到另一个 AppDomain 时,除了将 _data 字段作为透明代理传递给新的 AppDomain 之外,如何让 ClassA 以通常的方式进行序列化?
如有任何帮助,我们将不胜感激:)
Alright, I'm not sure if this question has been asked before so if it has then flame away. Lets say we have two classes like this
[Serializable]
public class ClassA
{
private string _name;
private ClassB _data;
}
public class ClassB : MarshalByRefObject
{
public string GetAppDomainName()
{
return AppDomain.Current.FriendlyName;
}
}
As you can see ClassA holds a reference to ClassB but class B inherits from the MarshalByRefObject class. My question is when I attempt to pass ClassA to another AppDomain how can I get ClassA to serialize the way it normally would except pass the _data field over to the new AppDomain as a transparent proxy?
Any help is appreciated :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
让 ClassA 从 MarshalByRefObject 派生,因为它是您想要远程到其他 AppDomain 的对象。
您可以实现 ISerialized http://msdn.microsoft .com/en-us/library/system.runtime.serialization.iserializing.aspx
跨应用程序域边界使用类型时,类型必须继承自 MarshalByRefObject,并且不得复制对象的状态因为对象的成员在创建它们的应用程序域之外不可用。
Have ClassA derive from MarshalByRefObject since it's the object you want to remote to the other AppDomain.
And you can implement ISerializable http://msdn.microsoft.com/en-us/library/system.runtime.serialization.iserializable.aspx
Types must inherit from MarshalByRefObject when the type is used across application domain boundaries, and the state of the object must not be copied because the members of the object are not usable outside the application domain where they were created.