Hello Codeproject,
I am currently attempting to serialize a piece of data. But all my application is doing is crashing itself with:
"The type initializer for 'Terrasylum.Terrasylum' threw an exception."
No details are given, and the last thing I added was:
private string CreateChunk(int ChunkX, int ChunkY)
{
StreamWriter _Writer = new StreamWriter(Path + ChunkX + "_" + ChunkY + ".txt");
Chunk _Chunk = new Chunk(ChunkX, ChunkY, ChunkSize, ChunkSize);
for (int x = 0; x < ChunkSize; x++)
{
for (int y = 0; y < ChunkSize; y++)
{
byte Block = ID.BLOCK_AIR;
if (ChunkY == 0 && y > ChunkSize / 2)
Block = ID.BLOCK_GRASS;
_Chunk.SetBlockID(x, y, Block);
}
}
string _ReturnString;
_ReturnString = SerializeToString(_Chunk);
_Writer.Write(_ReturnString);
_Writer.Flush();
_Writer.Close();
return _ReturnString;
}
This is how I serialize:
public static T SerializeFromString<T>(string xml)
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
using (StringReader reader = new StringReader(xml))
{
return (T)serializer.Deserialize(reader);
}
}
public static string SerializeToString(object obj)
{
XmlSerializer serializer = new XmlSerializer(obj.GetType());
using (StringWriter writer = new StringWriter())
{
serializer.Serialize(writer, obj);
return writer.ToString();
}
}
And these are my (Chunk and Block class):
[Serializable]
public class Chunk
{
private Block[,] _Blocks;
public int X { get; set; }
public int Y { get; set; }
public Chunk(int X, int Y, int Width, int Height)
{
_Blocks = new Block[Width, Height];
for (int i = 0; i < Width; i++)
{
for (int j = 0; j < Height; j++)
{
Block _Block = new Block(ID.BLOCK_AIR);
_Blocks[i,j] = _Block;
}
}
}
public void SetBlockID(int X, int Y, byte ID)
{
_Blocks[X, Y].ID = ID;
}
public byte GetBlockID(int X, int Y)
{
return _Blocks[X, Y].ID;
}
}
[Serializable]
public static class ID
{
public static int BlockSize = 32;
public const byte BLOCK_AIR = 30;
public const byte BLOCK_DIRT = 31;
public const byte BLOCK_GRASS = 32;
}
[Serializable]
public class Block
{
public byte ID { get; set; }
public Block(byte _ID)
{
ID = _ID;
}
}
Could anyone help me out ? If I get this fixed I can finally move on. :(