1

我有一些序列化代码设置如下:

    static void SerialiseObject(Object o, String path)
    {
        IFormatter formatter = new BinaryFormatter();
        Stream stream = new FileStream(path, FileMode.Create);
        formatter.Serialize(stream, o);
        stream.Close();
    }
    static Object DeserialiseObject(String path)
    {
        IFormatter formatter = new BinaryFormatter();
        Stream stream = new FileStream(path, FileMode.Open, FileAccess.Read);
        Object o = (Object)formatter.Deserialize(stream);
        stream.Close();
        return o;
    }

并且定义了以下成员的类:

[Serializable]
public class CircuitModel
{
    public Dictionary<String, Bus> Buses { protected set; get; }
    ...
}

我填充字典,然后以下代码成功序列化和反序列化字典,所有Bus对象都完好无损:

SerialiseObject(CircuitModel.Buses, "temp.bin");
Object o = DeserialiseObject("temp.bin");

但是当我尝试对以下内容做同样的事情时CircuitModel

SerialiseObject(CircuitModel, "temp.bin");
Object o = DeserialiseObject("temp.bin");

CircuitModel.Buses已初始化,但为空。

我也尝试过使用ISerializable(对于BusCircuitModel类)实现序列化并且遇到了完全相同的问题

知道为什么会发生这种情况吗?

4

3 回答 3

2

我认为您的子集合有更险恶的事情发生,因为类中字典的二进制序列化确实工作得很好。

    [TestFixture]
public class SerializeTest
{

    [Test]
    public void TestSer()
    {
        var parent = new Parent
                        {
                            Name = "Test"
                        };
        parent.Children.Add("Child1", new Child {Name = "Child1"});
        parent.Children.Add( "Child2", new Child { Name = "Child2" } );

        SerialiseObject(parent, "test.bin");
        var copy = DeserialiseObject("test.bin") as Parent;

        Assert.IsNotNull(copy);
        Assert.AreEqual(2, copy.Children.Count);
        Assert.IsTrue(copy.Children.ContainsKey("Child1"));
        Assert.AreEqual("Child1", copy.Children["Child1"].Name);
    }

    static void SerialiseObject( Object o, String path )
    {
        IFormatter formatter = new BinaryFormatter();
        Stream stream = new FileStream( path, FileMode.Create );
        formatter.Serialize( stream, o );
        stream.Close();
    }
    static Object DeserialiseObject( String path )
    {
        IFormatter formatter = new BinaryFormatter();
        Stream stream = new FileStream( path, FileMode.Open, FileAccess.Read );
        Object o = (Object) formatter.Deserialize( stream );
        stream.Close();
        return o;
    }

    [Serializable]
    private class Parent
    {
        public string Name { get; set; }
        public Dictionary<string, Child> Children { get; protected set; }

        public Parent()
        {
            Children = new Dictionary<string, Child>();
        }
    }

    [Serializable]
    private class Child
    {
        public string Name { get; set; }
    }
}

The children deserialize with the parent and contain the details they were initialized with. I would check any code that is setting your Buses collection. My example just did it in the constructor of the parent class, but it may be possible that you have rogue code setting it after it's been deserialized?

于 2012-11-22T22:05:57.793 回答
0

字典不可序列化。如果您需要序列化该数据,请删除字典,并将其替换为包含字典中数据的自定义类的列表:

[Serializable]
public class BusItem
{
    public string Name {get;set;}
    public Bus Bus {get;set;}
}

编辑:我刚刚发现您实际上可以使用 theDataContractSerializer来序列化字典。

http://theburningmonk.com/2010/05/net-tips-xml-serialize-or-deserialize-dictionary-in-csharp/

于 2012-11-22T21:44:49.657 回答
-1

如果您正在谈论 XML 序列化,可能是因为 Dictionary 不能序列化为 XML。看看为什么 .NET 中没有可序列化 XML 的字典

于 2012-11-22T21:49:45.007 回答