[XML Serialization]崩溃应用程序? [英] [XML Serialization] Crashes application ?
本文介绍了[XML Serialization]崩溃应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Hello Codeproject,
我目前正在尝试序列化一段数据。但是我的所有应用程序都在崩溃:
'Terrasylum.Terrasylum'的类型初始化程序引发异常。
没有给出详细信息,我添加的最后一件事是:
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:
// -- Create the Chunk.
/// <summary>
/// Creates the Chunk at the Cooardinate and returns the string..
/// </summary>
/// <param name="?"></param>
private string CreateChunk(int ChunkX, int ChunkY)
{
// -- Create a stream writer.
StreamWriter _Writer = new StreamWriter(Path + ChunkX + "_" + ChunkY + ".txt");
// -- Create a new Chunk.
Chunk _Chunk = new Chunk(ChunkX, ChunkY, ChunkSize, ChunkSize);
// -- Generate the Chunk.
for (int x = 0; x < ChunkSize; x++)
{
for (int y = 0; y < ChunkSize; y++)
{
// -- Block.
byte Block = ID.BLOCK_AIR;
if (ChunkY == 0 && y > ChunkSize / 2)
Block = ID.BLOCK_GRASS;
// -- Set the Block.
_Chunk.SetBlockID(x, y, Block);
}
}
// -- Get the XML string.
string _ReturnString;
_ReturnString = SerializeToString(_Chunk);
// -- Write.
_Writer.Write(_ReturnString);
// -- Close the Writer.
_Writer.Flush();
_Writer.Close();
// -- Return.
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();
}
}
这些是我的(Chunk和Block类):
And these are my (Chunk and Block class):
[Serializable]
public class Chunk
{
// -- Blocks.
private Block[,] _Blocks;
// -- Cooardinates.
public int X { get; set; }
public int Y { get; set; }
// -- Chunk.
/// <summary>
/// Initializes a new Chunk based on the Width and Height given.
/// </summary>
/// <param name="Width"></param>
/// <param name="Height"></param>
public Chunk(int X, int Y, int Width, int Height)
{
// -- Create the Block List.
_Blocks = new Block[Width, Height];
// -- Create all the Blocks.
for (int i = 0; i < Width; i++)
{
for (int j = 0; j < Height; j++)
{
// -- Create a new block.
Block _Block = new Block(ID.BLOCK_AIR);
// -- Add the block.
_Blocks[i,j] = _Block;
}
}
}
// -- Set a Block.
/// <summary>
/// Sets the block in the Chunk.
/// </summary>
/// <param name="X">X of the block.</param>
/// <param name="Y">Y of the block.</param>
/// <param name="ID">The ID of the block.</param>
public void SetBlockID(int X, int Y, byte ID)
{
// -- Set the Block.
_Blocks[X, Y].ID = ID;
}
// -- Get a block.
/// <summary>
/// Returns the ID of the given block.
/// </summary>
/// <param name="X">The X of the Block.</param>
/// <param name="Y">The Y of the Block.</param>
public byte GetBlockID(int X, int Y)
{
// -- Return.
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; }
// -- Block
/// <summary>
/// Initializes a new Block with the ID given.
/// </summary>
/// <param name="_ID">The ID of the block.</param>
public Block(byte _ID)
{
// -- Set the ID.
ID = _ID;
}
}
任何人都可以帮助我?如果我解决这个问题,我终于可以继续前进了。 :(
Could anyone help me out ? If I get this fixed I can finally move on. :(
推荐答案
序列化程序需要在反序列化类型上使用默认构造函数,即:
Serializers require a default constructor on types for deserialization i.e. :
[Serializable]
public class Chunk
{
public Chuck()
{
}
...
这篇关于[XML Serialization]崩溃应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文