Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 反序列化不';t重建属于类成员的词典_C#_.net_Serialization - Fatal编程技术网

C# 反序列化不';t重建属于类成员的词典

C# 反序列化不';t重建属于类成员的词典,c#,.net,serialization,C#,.net,Serialization,我已经设置了一些序列化代码,如下所示: static void SerialiseObject(Object o, String path) { IFormatter formatter = new BinaryFormatter(); Stream stream = new FileStream(path, FileMode.Create); formatter.Serialize(stream, o); stre

我已经设置了一些序列化代码,如下所示:

    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; }
    ...
}
但是,当我尝试对
电路模型
执行相同操作时:

SerialiseObject(CircuitModel, "temp.bin");
Object o = DeserialiseObject("temp.bin");
CircuitModel.bus
已初始化,但为空

我也尝试过用
ISerializable
(对于
总线
电路模型
类)实现序列化,但遇到了完全相同的问题


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

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

[Serializable]
public class BusItem
{
    public string Name {get;set;}
    public Bus Bus {get;set;}
}
编辑:我刚刚发现您实际上可以使用
DataContractSerializer
对字典进行序列化


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

[Serializable]
public class BusItem
{
    public string Name {get;set;}
    public Bus Bus {get;set;}
}
编辑:我刚刚发现您实际上可以使用
DataContractSerializer
对字典进行序列化


如果您谈论的是XML序列化,可能是因为字典不能序列化为XML。看看。

如果您谈论的是XML序列化,可能是因为字典不能序列化为XML。请看。

我认为您的子集合中有更险恶的东西,因为类中字典的二进制序列化确实可以正常工作

    [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; }
    }
}
[TestFixture]
公共类序列化测试
{
[测试]
公共void TestSer()
{
var parent=新父级
{
Name=“测试”
};
Add(“Child1”,新的子项{Name=“Child1”});
Add(“Child2”,新的子项{Name=“Child2”});
SerialiseObject(父对象,“test.bin”);
var copy=反序列化对象(“test.bin”)作为父对象;
Assert.IsNotNull(复制);
Assert.AreEqual(2,copy.Children.Count);
Assert.IsTrue(copy.Children.ContainsKey(“Child1”);
Assert.AreEqual(“Child1”,copy.childs[“Child1”].Name);
}
静态void SerialiseObject(对象o,字符串路径)
{
IFormatter formatter=新的BinaryFormatter();
Stream=新文件流(路径,FileMode.Create);
序列化(流,o);
stream.Close();
}
静态对象反序列化对象(字符串路径)
{
IFormatter formatter=新的BinaryFormatter();
Stream=新文件流(路径,FileMode.Open,FileAccess.Read);
对象o=(对象)格式化程序。反序列化(流);
stream.Close();
返回o;
}
[可序列化]
私有类父类
{
公共字符串名称{get;set;}
公共字典子项{get;protected set;}
公共家长()
{
儿童=新字典();
}
}
[可序列化]
私班儿童
{
公共字符串名称{get;set;}
}
}

子项与父项反序列化,并包含初始化时使用的详细信息。我会检查设置您的
bus
集合的任何代码。我的示例只是在父类的构造函数中实现了这一点,但在反序列化之后,可能有流氓代码对其进行了设置?

我认为您的子集合中发生了更危险的事情,因为类中字典的二进制序列化工作正常

    [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; }
    }
}
[TestFixture]
公共类序列化测试
{
[测试]
公共void TestSer()
{
var parent=新父级
{
Name=“测试”
};
Add(“Child1”,新的子项{Name=“Child1”});
Add(“Child2”,新的子项{Name=“Child2”});
SerialiseObject(父对象,“test.bin”);
var copy=反序列化对象(“test.bin”)作为父对象;
Assert.IsNotNull(复制);
Assert.AreEqual(2,copy.Children.Count);
Assert.IsTrue(copy.Children.ContainsKey(“Child1”);
Assert.AreEqual(“Child1”,copy.childs[“Child1”].Name);
}
静态void SerialiseObject(对象o,字符串路径)
{
IFormatter formatter=新的BinaryFormatter();
Stream=新文件流(路径,FileMode.Create);
序列化(流,o);
stream.Close();
}
静态对象反序列化对象(字符串路径)
{
IFormatter formatter=新的BinaryFormatter();
Stream=新文件流(路径,FileMode.Open,FileAccess.Read);
对象o=(对象)格式化程序。反序列化(流);
stream.Close();
返回o;
}
[可序列化]
私有类父类
{
公共字符串名称{get;set;}
公共字典子项{get;protected set;}
公共家长()
{
儿童=新字典();
}
}
[可序列化]
私班儿童
{
公共字符串名称{get;set;}
}
}

子项与父项反序列化,并包含初始化时使用的详细信息。我会检查设置您的
bus
集合的任何代码。我的示例只是在父类的构造函数中实现了这一点,但在反序列化之后,可能有流氓代码对其进行了设置?

是的,我已经序列化字典很多年了。它们与DataContractSerializer和BinaryFormatter配合使用也很好。问题在于字典不是一个类中只序列化一次是的,我已经序列化字典很多年了。它们与DataContractSerializer和BinaryFormatter配合使用也很好。问题在于字典不能只在c中序列化一次