【问题标题】:Deserializtion returns a field of a class instead of the class反序列化返回类的字段而不是类
【发布时间】:2016-07-17 21:40:48
【问题描述】:

我有一个名为 Schematic 的类,它存储了我的游戏中使用的块结构。我试图找到一种使用 BinaryFormatter 保存和加载它们的方法,但是我在反序列化方面遇到了问题。当我反序列化时,我无法转换为我的源类型,而是只让我获得一个字段,一个二维整数数组。

这是原理图类的代码:

[Serializable]
public class Schematic
{
    public static Schematic BlankSchematic = new Schematic("BLANK");
    public int[,] Blocks;
    public V2Int Size;
    public V2Int Location = V2Int.zero;

    public string Name;

    //---PROPERTIES---
    //lower is more rare
    public int Rarity = 100;
    //---END PROPERTIES---

    public Schematic(string name)
    {
        Name = name;
    }
    public Schematic(string name, int[,] blocks)
    {
        Name = name;
        ModifyBlockArray(blocks);
    }
    public void ModifyBlockArray(int[,] newBlocks)
    {
        Blocks = newBlocks;
        Size = new V2Int(newBlocks.GetLength(0), newBlocks.GetLength(1));
    }
}

我的方法在一个单独的类中用于序列化和反序列化:

public void SaveSchematic(Schematic schem)
{
    using (Stream stream = new FileStream(SchematicsDirectory + "/" + schem.Name + ".schem", FileMode.Create, FileAccess.Write, FileShare.None))
    {
        BinaryFormatter bf = new BinaryFormatter();
        Debug.Log(schem.GetType());
        bf.Serialize(stream, schem);
    }

}

public void LoadSchematics(string dir)
{
    BinaryFormatter bf = new BinaryFormatter();

    DirectoryInfo info = new DirectoryInfo(dir);
    FileInfo[] fileinfo = info.GetFiles("*.schem");
    for (int i = 0; i < fileinfo.Length; i++)
    {
        FileStream fs = new FileStream(dir + fileinfo[i].Name, FileMode.Open);
        object tempO = bf.Deserialize(fs);
        Debug.Log(tempO + ", " + tempO.GetType());
        Schematic temp = (Schematic)tempO;
        SchematicsByName.Add(temp.Name, temp);
        Schematics.Add(temp);
        print("Loaded Schematic: " + temp.Name);
        fs.Close();
        fs.Dispose();
    }
}

这很奇怪,因为当我查看序列化文件时,我会看到其他字段和类名“Schematic”。这是一个小的示例文件:

    ÿÿÿÿ          Assembly-CSharp       Schematic   BlocksSizeLocationNameRarity System.Int32[,]V2Int   V2Int             V2Int   xy                            
TestSavingd                                           

V2Int 也被标记为可序列化。当我反序列化时,我得到的是 Blocks 数组而不是整个类,这真的很奇怪。任何帮助将不胜感激。

这是我第一次在这里发帖,如有错误请见谅。

【问题讨论】:

  • 我在任何地方都看不到任何错误,这是非常适合第一个问题!做得好! -- 我添加了二进制序列化标签,认为在标签中指定序列化类型可能会很方便。
  • 请同时发布 V2Int 类的定义

标签: c# serialization deserialization binary-serialization


【解决方案1】:

我用一个示例 sn-p 进行了尝试,它为我正确地序列化和反序列化。请确保发送的原理图对象具有所有正确的值。一般来说,

反序列化的对象是最终值的最佳选择。不要看序列化文件。 此外,如果您反序列化了错误的类型,则此行通常会引发强制转换异常。

Schematic temp = (Schematic)tempO;

所以请执行下面的sn-p,让我们知道。 它对我来说很好。 (我根据它的构造函数制作了一个随机的 V2Int 类)

    public static string SchematicsDirectory = "d:\\temp\\s";

    static void Main(string[] args)
    {
        var p = new Program();

        var array = new int[2, 2];
        array[0, 0] = 1;
        array[0, 1] = 2;
        array[1, 0] = 3;
        array[1, 1] = 4;

        var testObject = new Schematic("fezzik", array);

        p.SaveSchematic(testObject);
        p.LoadSchematics(SchematicsDirectory + "/");
    }

【讨论】:

  • 不知道为什么它不能更早地工作,只是再次尝试(没有更改),现在它可以工作了。也许我的电脑只是疯了或什么哈哈。感谢您的回复。
  • 酷。作为附加输入,您不需要执行 fs.close、fs.dispose.. c# 使用 'using' 关键字更容易.. auto-disposable.. using (var fs = new FileStream(dir + fileinfo[ i].Name, FileMode.Open)) { object tempO = bf.Deserialize(fs); Debug.Log(tempO + ", " + tempO.GetType());原理图温度 = (原理图)tempO; SchematicsByName.Add(temp.Name, temp); Schematics.Add(temp); print("加载的原理图:" + temp.Name); }
猜你喜欢
  • 2022-06-12
  • 2019-02-20
  • 2018-08-20
  • 2018-06-29
  • 1970-01-01
  • 2011-01-13
  • 1970-01-01
  • 2012-08-10
  • 2019-03-11
相关资源
最近更新 更多