DataContractSerializer的序列化不是类的成员,它继承了ISerializable [英] DataContractSerializer not Serializing member of class that inherits ISerializable

查看:137
本文介绍了DataContractSerializer的序列化不是类的成员,它继承了ISerializable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个类:

using System;
using System.Collections.Generic;
using System.Runtime.Serialization;

namespace Grouping
{
    [Serializable]
    public class Group<T> : HashSet<T>
    {
        public Group(string name)
        {
            this.name = name;
        }

        protected Group(){}

        protected Group(SerializationInfo info, StreamingContext context):base(info,context)
        {
            name = info.GetString("koosnaampje");
        }

        public override void GetObjectData(SerializationInfo info,StreamingContext context)
        {
            base.GetObjectData(info,context);
            info.AddValue("koosnaampje", Name);
        }

        private string name;
        public string Name
        {
            get { return name; }
            private set { name = value; }
        }
    }
}

当它从HashSet的继承它必须实现ISerializable的,因此受保护的构造和GetObjectData方法。 以前我和序列化与反序列化的BinaryFormatter该类成功地。

As it inherits from HashSet it has to implement ISerializable, hence the protected constructor and GetObjectData method. Formerly I serialized and deserialized this class succesfully with the BinaryFormatter.

由于我希望能够检查由我想切换到DataContractSerializer的序列化生成的输出。

Because I want to be able to inspect the output that is generated by the serializer I want to switch to the DataContractSerializer.

我写这篇测试:

[TestMethod]
public void SerializeTest()
{
    var group = new Group<int>("ints"){1,2,3};
    var serializer = new DataContractSerializer(typeof (Group<int>));
    using (var stream=File.OpenWrite("group1.xml"))
    {
        serializer.WriteObject(stream,group);
    }
    using (var stream=File.OpenRead("group1.xml"))
    {
        group = serializer.ReadObject(stream) as Group<int>;
    }
    Assert.IsTrue(group.Contains(1));
    Assert.AreEqual("ints",group.Name);
}

测试失败,因为Name属性为空! (整数是序列化的(de)正确虽然) 这是怎么回事?

The test fails because the Name property is null! (the integers are (de)serialized correctly though) What is happening?

编辑:这无关名为支持字段是私有的。使之成为公众有相同的结果。

it has nothing to do with the name backing field being private. Making it public has the same result.

推荐答案

这是无关 ISerializable的; 的DataContractSerializer 根本就不使用 ISerializable的(它会使用的IXmlSerializable ,但你不想这样做...)

This is nothing to do with ISerializable; DataContractSerializer simply doesn't use ISerializable (it will use IXmlSerializable, but you don't want to do that...)

大多数串行化器,包括的XmlSerializer 的DataContractSerializer (和数据绑定,对于这个问题),治疗集合为不同的实体。它可以是一个其他,但不能两者兼得。因为它检测到它是一个集,它系列化的内容(即无论是在设置),而不是属性(名称等)。

Most serializers, including XmlSerializer and DataContractSerializer (and data-binding, for that matter), treat collections as different to entities. It can be one or the other, but not both. Because it detects that it is a "collection", it serializes the contents (i.e. whatever is in the set), not the properties (Name etc).

您应该的封装的集合,而不是继承的吧。

You should encapsulate a collection, rather than inherit it.

此外,正确使用的DataContractSerializer ,这将是明智的添加 [数据成员] / [DataContract ] 属性。例如:

Also; to correctly use DataContractSerializer, it would be wise to add the [DataMember]/[DataContract] attributes. For example:

[Serializable, DataContract] // probably don't need [Serializable]
public class Group<T>
{
    [DataMember]
    public HashSet<T> Items { get; private set; }

    protected Group()
    {
        Items = new HashSet<T>();
    }
    public Group(string name) : this()
    {
        Name = name;
    }
    [DataMember]
    public string Name {get ;private set;}
}

这篇关于DataContractSerializer的序列化不是类的成员,它继承了ISerializable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆