序列化 POCO 排除类成员

发布于 2024-07-25 02:35:26 字数 126 浏览 3 评论 0原文

我希望创建一个 POCO [Serialized],但不创建其类层次结构树中的任何其他类成员。 我知道 [NonSerialized] 只适用于字段,但是有没有办法排除它们或在 POCO 上使用 [Serialized] 选择特定成员?

I wish to make a POCO [Serializable] but not any other class members in its class hierarchy tree. I know there is [NonSerialized] which works only for fields, but is there any way to exclude them or choose specific members using [Serializable] on the POCO?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恋竹姑娘 2024-08-01 02:35:26

您应该查看 DataContractSerializer; 它使用“选择加入”方法进行序列化。

另外,阅读 XmlSerializer 与 DataContractSerializer:Wcf 中的序列化 示例以及 DataContractSerializerXmlSerializer 之间的比较。

XmlSerializer 已在 .Net 中
自 1.0 版本以来一直为我们服务
适用于远程处理、Web 等所有内容
服务、序列化到文件等。
然而在 .Net 3.0 中
DataContractSerializer 出现了。
突然间有很多指导
建议我们应该使用它
旧的、经过验证的、真正的 XmlSerializer。
Wcf 甚至使用它作为默认值
序列化机制。 这
问题是,“真的更好吗?”。
结论是是,也不是。 最喜欢
这取决于你的事情
实施以及您需要什么。 为了
Wcf,你应该更喜欢使用
数据契约序列化器。 如果你需要
完全控制 xml 的外观
不过,你应该回到
XmlSerializer。

You should look at the DataContractSerializer; it uses an "opt-in" approach to serialization.

Also it would be a good idea to read XmlSerializer vs DataContractSerializer: Serialization in Wcf to for examples and a comparison between DataContractSerializer and XmlSerializer.

The XmlSerializer has been in .Net
since version 1.0 and has served us
well for everything from Remoting, Web
Services, serializing to a file, etc.
However in .Net 3.0 the
DataContractSerializer came along.
And all of a sudden a lot of guidance
suggests that we should use it over
the old tried and true XmlSerializer.
Wcf even uses this as the default
mechanism for serialization. The
question is, “Is it really better?”.
The verdict is yes, and no. Like most
things it depends on your
implementation and what you need. For
Wcf, you should prefer to use the
DataContractSerializer. If you need
full control over how the xml looks
though, you should go back to the
XmlSerializer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文