試圖.dat文件轉換爲自己的類投對象類C#
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。該文件的路徑是正確的,類也相匹配,但不知道爲什麼「(0級)對象」不工作...
感謝
還使用了BinaryFormatter的序列化'Level0'類型?只是爲了科學:你可以用'return new Level0();'替換'return null;'。我想驗證路徑是否正確。 – rene
沒有什麼改變,文件存在,它確實返回對象,但不能將其轉換爲Level0()並且是我使用BinaryFormatter來序列化 – MJahongir
您可以提供Level0類的其他信息。它是否標記爲可序列化?它是否包含也是自定義類型的其他成員? – Vect0rZ