title: XML serialization of list of class containing a System.Enum field causing crash
body: <p>I have a custom type with two enum members. I'm trying to create a list of this custom type, then serialize that list to XML. When I try and do this, Unity (I'm working in Unity) crashes, which it's never done on me before. </p>
  
  <p>I've narrowed the issue down to this from my serialization code:</p>
  
  <pre><code>xmls.Serialize(stream, dataCollection);
  </code></pre>
  
  <p>So the issue poccurs when the XMLSerializer attempts to serialize my List. I have no idea why though! So any help would be appreciated. </p>
  
  <p>Code below.</p>
  
  <p><strong>XML Serialization code</strong></p>
  
  <pre><code>public static void WriteGenericCollectionToXML&lt;T&gt;(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);
          }
      }
  </code></pre>
  
  <p><strong>Code to create list of objects of my custom type</strong></p>
  
  <pre><code>public  List&lt;BlockType&gt; t = new List&lt;BlockType&gt;();
  
          t.Add(new BlockType(true));
          t.Add(new BlockType(true));
          t.Add(new BlockType(true));
  
          SaveLoad.WriteGenericCollectionToXML(t, Application.dataPath + "/test.xml");
  </code></pre>
  
  <p><strong>Custom type</strong></p>
  
  <pre><code>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 &amp;&amp; test.subType == this.subType);
      }
  
      public override int GetHashCode ()
      {
          return this.GetHashCode ();
      }
  }
  </code></pre>