简体   繁体   English

XmlInclude属性和派生类

[英]XmlInclude attribute and derived classes

I'm using Xml serialization to persist some objects on disk. 我正在使用Xml序列化来保存磁盘上的某些对象。

The class structure is the following: 类结构如下:

XmlInclude(typeof(BranchExplorerViewInfo))
public class ViewInfo
{
   ...
}

public class BranchExplorerViewInfo : ViewInfo
{
   ...
}

public class CustomBranchExplorerViewInfo: BranchExplorerViewInfo
{
   ...
}

Then, I need the following behavior: 然后,我需要以下行为:

BranchExplorerViewInfo view = new BranchExplorerViewInfo();
view.GetType().IsSerializable; //I need this to be TRUE

CustomBranchExplorerViewInfo customView = new CustomBranchExplorerViewInfo();
customView.GetType().IsSerializable; //I need this to be FALSE

So, I want BranchExplorerViewInfo to be serializable but CustomBranchExplorerViewInfo to be non-serializable. 所以,我希望BranchExplorerViewInfo可序列化,但CustomBranchExplorerViewInfo是不可序列化的。 Is there any attribute to exclude a type/class? 是否有任何属性可以排除类型/类?

Thanks in advance. 提前致谢。

You are confusing two totally different types of serialization. 您混淆了两种完全不同类型的序列化。

On the one hand you are talking about [XmlInclude] which is related to xml serialization. 一方面,您正在谈论与xml序列化相关的[XmlInclude]

On the other hand you are testing Type.IsSerializable which is related to binary serialization (ie related to the [Serializable] attribute, and the BinaryFormatter class). 另一方面,您正在测试Type.IsSerializable ,它与二进制序列化有关(即与[Serializable]属性相关,以及BinaryFormatter类)。

Although these are both types of serialization, they are very different and unrelated. 虽然这些都是序列化的类型,但它们是非常不同且不相关的。

There isn't any simple equivalent test of "IsXmlSerialization" that I can think of. 我能想到的“IsXmlSerialization”没有任何简单的等效测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM