反序列化 returns 一个字段 class 而不是 class
Deserializtion returns a field of a class instead of the class
我有一个名为 Schematic 的 class,它存储了在我的游戏中使用的块结构。我正在尝试找到一种使用 BinaryFormatter 保存和加载它们的方法,但是我在反序列化方面遇到了问题。当我反序列化时,我无法转换为我的源类型,而是它只让我得到一个字段,一个二维整数数组。
这是原理图的代码class:
[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));
}
}
我的方法在单独的 class 中用于序列化和反序列化:
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();
}
}
这很奇怪,因为当我查看序列化文件时,我看到其他字段和 class 名称 "Schematic." 这是一个小示例文件:
ÿÿÿÿ Assembly-CSharp Schematic BlocksSizeLocationNameRarity System.Int32[,]V2Int V2Int V2Int xy
TestSavingd
V2Int 也被标记为可序列化。当我反序列化时,我得到的是 Blocks 数组而不是整个 class,这真的很奇怪。任何帮助将不胜感激。
这是我第一次post在这里,如果有任何错误,请见谅。
我尝试了一个示例片段,它为我正确地序列化和反序列化了。请确保发送的 Schematic 对象具有所有正确的开始值。一般来说,
反序列化对象是最终值的最佳选择。不要看序列化文件。
此外,如果反序列化的类型错误,此行通常会抛出强制转换异常。
Schematic temp = (Schematic)tempO;
所以请执行以下代码段并告诉我们。
它对我有用。 (我根据其构造函数编写了一个随机的 V2Int class)
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 + "/");
}
我有一个名为 Schematic 的 class,它存储了在我的游戏中使用的块结构。我正在尝试找到一种使用 BinaryFormatter 保存和加载它们的方法,但是我在反序列化方面遇到了问题。当我反序列化时,我无法转换为我的源类型,而是它只让我得到一个字段,一个二维整数数组。
这是原理图的代码class:
[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));
}
}
我的方法在单独的 class 中用于序列化和反序列化:
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();
}
}
这很奇怪,因为当我查看序列化文件时,我看到其他字段和 class 名称 "Schematic." 这是一个小示例文件:
ÿÿÿÿ Assembly-CSharp Schematic BlocksSizeLocationNameRarity System.Int32[,]V2Int V2Int V2Int xy
TestSavingd
V2Int 也被标记为可序列化。当我反序列化时,我得到的是 Blocks 数组而不是整个 class,这真的很奇怪。任何帮助将不胜感激。
这是我第一次post在这里,如果有任何错误,请见谅。
我尝试了一个示例片段,它为我正确地序列化和反序列化了。请确保发送的 Schematic 对象具有所有正确的开始值。一般来说,
反序列化对象是最终值的最佳选择。不要看序列化文件。 此外,如果反序列化的类型错误,此行通常会抛出强制转换异常。
Schematic temp = (Schematic)tempO;
所以请执行以下代码段并告诉我们。 它对我有用。 (我根据其构造函数编写了一个随机的 V2Int class)
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 + "/");
}