尝试将.dat文件转换为其自身的类
问题:LoadObjInBinary返回null。文件路径正确,类也匹配,但不知道为什么"(Level0) object"不起作用...
谢谢
level0 = (Level0) LoadObjInBinary(level0, "Level" + levelNumber);
public static object LoadObjInBinary(object myClass, string fileName) {
fileName += ".dat";
if (File.Exists(FileLocation + fileName)) {
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(FileLocation + fileName, FileMode.Open);
myClass = bf.Deserialize(file);
file.Close();
return myClass;
} else {
return null;
}
}
Level() class
[Serializable]
public class Level0 { //Using this class to create Level.dat binary file
static int level = 1;
static int moves = 15;
static int seconds;
static int minScoreForOneStar = 1000;
static int minScoreForTwoStars = 1500;
static int minScoreForThreeStars = 2000;
static TargetObj[] targetObjs = {new TargetObj(Targets.Black, 10), new TargetObj(Targets.Freezer, 1), new TargetObj(Targets.Anchor, 2)};
static Color[] colors = {Constants.grey, Constants.green, Constants.pink, Constants.brown, Constants.purple, Constants.lightBlue};
static Cell[,] levelDesign;
//the rest is Properties of Fields
}
问题:LoadObjInBinary返回null。文件路径正确,类也匹配,但不知道为什么"(Level0) object"不起作用...
谢谢
return null;
替换为return new Level0();
。 - rene