Pysassin Pysassin - 3 years ago 126
C# Question

C# Serialization Woes - Int Array not saving/returning correctly

I was hoping some one could enlighten me on what I am doing wrong, and the best way to correct it (or point me to a link that does).

I have the below code/classes I am using to serialize my data using a BinaryFormatter. For some reason the first int (Corners) in the TileData class is deserializing back as 255 all the time. I have verified that the data is saved in the structure correctly before serialization so some where between the serialization and deserialization something is happening with the data, and I'm unsure why or how to check which end it is.

Any thoughts?

[Serializable]
public class GameSaveData
{
public readonly string Name;
public readonly int[] LevelSettings;
public readonly int[] GTime;
public readonly ChunkData[] Data;

public GameSaveData(string _name, int[] _settings, int[] _time, ChunkData[] _chunks)
{
Name = _name;
LevelSettings = _settings;
GTime = _time;
Data = _chunks;
}
}

[Serializable]
public class TileData
{
public readonly int Corners;
public readonly int TypeID;
public readonly int[] FloorSpecs; // 0 -- Floor Missing, 1 - Floor Type ID, 2 -- SubFloor Type

public TileData(int _c, int _t, int[] _floorSpecs)
{
Corners = _c;
TypeID = _t;
FloorSpecs = _floorSpecs;
}
}

[Serializable]
public class ChunkData
{
public readonly int[] Position;
public readonly TileData[] Data;

public ChunkData(Vector3 _pos, TileData[] _data)
{
Position = new int[] { (int)_pos.x, (int)_pos.y, (int)_pos.z };
Data = _data;
}
}

Answer Source

A serizable Class must be a simple container with a parameterless constructor (only at Xml Serialization).

Have a look at this post.

Something like this

[Serializable]
public class GameSaveData
{
    public string Name { get; set; }
    public int[] LevelSettings { get; set; }
    public int[] GTime { get; set; }
    public ChunkData[] Data { get; set; }

    private GameSaveData() 
    {
        // parameter less constrctor
    }

    public GameSaveData(string _name, int[] _settings, int[] _time, ChunkData[] _chunks)
    {
        Name = _name;
        LevelSettings = _settings;
        GTime = _time;
        Data = _chunks;
    }
}

[Serializable]
public class TileData
{
    public int Corners { get; set; }
    public int TypeID { get; set; }
    public int[] FloorSpecs { get; set; } // 0 -- Floor Missing, 1 - Floor Type ID, 2 -- SubFloor Type

    public TileData()
    {
        // parameter less constrctor            
    }

    public TileData(int _c, int _t, int[] _floorSpecs)
    {
        Corners = _c;
        TypeID = _t;
        FloorSpecs = _floorSpecs;
    }
}

[Serializable]
public class ChunkData
{
    public readonly int[] Position { get; set; }
    public readonly TileData[] Data { get; set; }

    public ChunkData()
    {
        // parameter less constrctor                        
    }

    public ChunkData(Vector3 _pos, TileData[] _data)
    {
        Position = new int[] { (int)_pos.x, (int)_pos.y, (int)_pos.z };
        Data = _data;
    }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download