C# XML 序列化 - 弱化封装?

发布于 2024-12-18 04:43:26 字数 256 浏览 0 评论 0原文

我是否正确地认为,为了让 C# 序列化对象,我必须为每个需要存储其状态的字段拥有一个公共属性?

如果是这样,那不是很糟糕吗,因为它削弱了(如果不是完全破坏的话)我的类所拥有的任何封装?

在 Java 中,XStream 可以迭代每个非瞬态字段并将其存档。在 C# 中,这种情况不可能发生,更糟糕的是,字典之类的东西根本不会序列化。一切都有点混乱,不是吗?

我已经看到了 XStream 到 .net 的“端口”的 DLL,但没有文档,我对此表示怀疑。

Am I correct in thinking that, in order to get C# to serialize an object, I MUST have a public property for every field that needs its state stored?

If so, is that not very very sucky, as it weakens (if not breaks entirely) any encapsulation my class has?

In Java, XStream can iterate over every non-transient field and archive it. In C# this can't happen, and just to make things worse, things like Dictionaries don't serialize AT ALL. It's all a bit of a mess, no?

I've seen the DLL for a "port" of XStream to .net, but there are no docs and I'm suspicious.

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

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

发布评论

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

评论(2

擦肩而过的背影 2024-12-25 04:43:26

您应该使用 DataContractSerializer,并使用 [DataMember] 标记要序列化的每个属性/字段。它并不关心您的字段是私有的还是公共的。顺便说一句,您可以用它序列化字典...

[DataContract]
public class MyClass
{
    [DataMember]
    private string _privateField;

    [DataMember]
    public int PublicProperty { get; set;}
}

序列化:

private static string SerializeXml<T>(T item)
{
    DataContractSerializer ser = new DataContractSerializer(item.GetType());

    StringBuilder sb = new StringBuilder();
    XmlWriterSettings settings = new XmlWriterSettings { OmitXmlDeclaration = true, ConformanceLevel = ConformanceLevel.Fragment };
    using (XmlWriter writer = new XmlWriter(sb, settings))
    {
        ser.WriteObject(writer, item);
    }
    return sb.ToString();
}

在此处查看 XmlSerializer 和 DataContractSerializer 之间的差异:http://www.danrigsby.com/blog/index.php/2008/03/07/xmlserializer-vs-datacontractserializer-serialization-in-wcf/

You should use DataContractSerializer, and mark every property/field you want to serialize with [DataMember]. It doesn't care if your fields are private or public. By the way you can serialize Dictionaries with it...

[DataContract]
public class MyClass
{
    [DataMember]
    private string _privateField;

    [DataMember]
    public int PublicProperty { get; set;}
}

Serialization :

private static string SerializeXml<T>(T item)
{
    DataContractSerializer ser = new DataContractSerializer(item.GetType());

    StringBuilder sb = new StringBuilder();
    XmlWriterSettings settings = new XmlWriterSettings { OmitXmlDeclaration = true, ConformanceLevel = ConformanceLevel.Fragment };
    using (XmlWriter writer = new XmlWriter(sb, settings))
    {
        ser.WriteObject(writer, item);
    }
    return sb.ToString();
}

Look here for differences between XmlSerializer and DataContractSerializer : http://www.danrigsby.com/blog/index.php/2008/03/07/xmlserializer-vs-datacontractserializer-serialization-in-wcf/

吹梦到西洲 2024-12-25 04:43:26

二进制格式化程序可以序列化私有字段甚至只读字段,而无需属性。 XmlSerializer 只能使用公共无参数构造函数和公共属性进行序列化。如果您想将 XmlSerializer 与封装一起使用,您可以使用 IXmlSerialized,但这相当痛苦。

如果您的对象模型相当简单,或者您可以通过引入特殊的 DTO:s 进行序列化(例如避免结构)来使其变得相当简单,那么我建议使用基于契约的序列化器,它可以序列化私有字段或属性。看看 protobuf-net。

The binaryformatter serializes private and even readonly fields without the need for properties. The XmlSerializer can only serialize with a public no-arg constructor and public properties. If you want to use XmlSerializer with encapsulation you can use IXmlSerializable, but that is rather painful.

If your object model is fairly simple or you can make it fairly simple by introducing special DTO:s for serialization (e.g. to avoid structs), then I recommend using a contract based serializer that can serialize private fields or properties. Have a look at protobuf-net.

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