我有一個具有兩個枚舉成員的自定義類型。我試圖創建這個自定義類型的列表,然後將該列表序列化爲XML。當我嘗試做這件事時,Unity(我在Unity工作)崩潰,這在我之前從未做過。包含導致崩潰的System.Enum字段的類列表的XML序列化
我已經縮小問題到這個從我的序列化代碼:
xmls.Serialize(stream, dataCollection);
所以當XmlSerializer的試圖序列名單的問題poccurs。我不知道爲什麼!所以任何幫助,將不勝感激。
下面的代碼。
XML序列化代碼
public static void WriteGenericCollectionToXML<T>(T dataCollection, string filePath) where T : IEnumerable, ICollection
{
//Check to see if file already exists
if(!File.Exists(filePath))
{
//if not, create it
File.Create(filePath);
}
try
{
XmlSerializer xmls = new XmlSerializer(typeof(T));
using(Stream stream = new FileStream(filePath, FileMode.Append))
{
xmls.Serialize(stream, dataCollection);
}
}
catch(Exception e)
{
Debug.LogException(e);
}
}
代碼來創建我的自定義類型的對象列表
public List<BlockType> t = new List<BlockType>();
t.Add(new BlockType(true));
t.Add(new BlockType(true));
t.Add(new BlockType(true));
SaveLoad.WriteGenericCollectionToXML(t, Application.dataPath + "/test.xml");
自定義類型
using UnityEngine;
using System.Collections;
using System.Xml;
using System.Xml.Serialization;
public enum BaseBlockType
{
[XmlEnum(Name = "Animals")]Animals,
[XmlEnum(Name = "Geometry")]Geometry,
[XmlEnum(Name = "Letters")]Letters
}
public enum LetterBlockType
{
[XmlEnum(Name = "A")]A,
[XmlEnum(Name = "B")]B,
[XmlEnum(Name = "C")]C,
[XmlEnum(Name = "D")]Z,
[XmlEnum(Name = "E")]X,
[XmlEnum(Name = "F")]F,
[XmlEnum(Name = "G")]G
}
public enum AnimalType
{
[XmlEnum(Name = "Elephant")]Elephant,
[XmlEnum(Name = "Giraffe")]Giraffe,
[XmlEnum(Name = "Tiger")]Tiger,
[XmlEnum(Name = "Sheep")]Sheep
}
public enum GeometaryType
{
[XmlEnum(Name = "Square")]Square,
[XmlEnum(Name = "Triangle")]Triangle,
[XmlEnum(Name = "Circle")]Circle,
[XmlEnum(Name = "Star")]Star
}
[XmlType]
public class BlockType
{
[XmlAttribute]
public BaseBlockType baseType;
[XmlAttribute]
public System.Enum subType;
public BlockType(BaseBlockType baseT, System.Enum subT)
{
//Set base type
baseType = baseT;
//Set sub type
subType = subT;
//possibly need to perform checks that sub-type is acceptable
}
public BlockType(BaseBlockType baseT)
{
//Set base type
baseType = baseT;
//Set sub type
subType = RandSubType(baseType);
}
public BlockType(bool random)
{
if(random)
{
//Set base type
int enumLength = System.Enum.GetValues(typeof(BaseBlockType)).Length;
int rand = Random.Range(0, enumLength);
baseType = (BaseBlockType)rand;
//Set sub type
subType = RandSubType(baseType);
}
}
public BlockType()
{}
public System.Enum RandSubType(BaseBlockType baseType)
{
int subEnumLength;
int subRand;
switch(baseType)
{
case BaseBlockType.Animals:
subEnumLength = System.Enum.GetValues(typeof(AnimalType)).Length;
subRand = Random.Range(0, subEnumLength);
return (AnimalType)subRand;
case BaseBlockType.Geometry:
subEnumLength = System.Enum.GetValues(typeof(GeometaryType)).Length;
subRand = Random.Range(0, subEnumLength);
return(GeometaryType)subRand;
case BaseBlockType.Letters:
subEnumLength = System.Enum.GetValues(typeof(LetterBlockType)).Length;
subRand = Random.Range(0, subEnumLength);
return (LetterBlockType)subRand;
default:
Debug.Log("Block Sub Type Selection not working");
return null;
}
}
public override string ToString()
{
string result = baseType.ToString() + "." + subType.ToString();
return result;
}
public override bool Equals (object t)
{
var test = t as BlockType;
if(t == null)
return false;
return(test.baseType == this.baseType && test.subType == this.subType);
}
public override int GetHashCode()
{
return this.GetHashCode();
}
}
圍繞序列化的try/catch - 它寫入的異常是什麼?如果沒有任何異常在你的代碼的入口點附近放一個try/catch,看看它說了什麼 –
剛剛嘗試在try catch中包裝問題行,它仍然崩潰Unity!很煩人。 –
嘗試AppDomain.CurrentDomain.UnhandledException + =(s,e)=> Console.WriteLine(e); –