XmlSerializer实例中的StackOverFlowError

时间:2013-12-13 11:50:42

标签: c# generics inheritance xmlserializer

当Child类调用父类约束器时,我不知道为什么会出现Stackoverflow错误?!

首先:抱歉我的长码,因为我的一些朋友要求它,所以我把它给你了!

public  class XMLCollection<T>: IList<T> where T: new ()
{
   private XmlSerializer ser;

   private List<T> InnerList= new List<T>();

   public XMLCollection()
   {
       ser = new XmlSerializer(this.GetType(), new XmlRootAttribute(this.GetType().Name ));
   }

   /// <summary>
   /// 
   /// </summary>
   /// <param name="path">path format: @"D:\FolderName"</param>
   public void SaveToXML(string path)
   {
       try
       {
           using (StreamWriter sw = new StreamWriter(path+ "\\"+this.GetType().Name))
           {
               ser.Serialize(sw, this);
           }
       }
       catch (Exception ex)
       {

           Console.WriteLine(ex.Message );
       }


   }

   public void LoadFromXML(string FullPath)
   {
       try
       {
           if (File.Exists(FullPath))
           {
               using (StreamReader sr= new StreamReader(FullPath))
               {
                   this.InnerList=((XMLCollection<T>)  ser.Deserialize(sr)).InnerList ;
               }
           }
           else
           {
               Console.WriteLine("File not exist....");
           }
       }
       catch (Exception ex)
       {

           Console.WriteLine(ex.Message );
       }

   }
   #region Ilist
   public int IndexOf(T item)
   {
       throw new NotImplementedException();
   }

   public void Insert(int index, T item)
   {
       throw new NotImplementedException();
   }

   public void RemoveAt(int index)
   {
       throw new NotImplementedException();
   }

   public T this[int index]
   {
       get
       {
           return InnerList[index];
       }
       set
       {
           InnerList[index] = value;
       }
   }

   public void Add(T item)
   {
       InnerList.Add(item);
   }

   public void Clear()
   {
       InnerList.Clear();
   }

   public bool Contains(T item)
   {
       throw new NotImplementedException();
   }

   public void CopyTo(T[] array, int arrayIndex)
   {
       throw new NotImplementedException();
   }

   public int Count
   {
       get { throw new NotImplementedException(); }
   }

   public bool IsReadOnly
   {
       get { throw new NotImplementedException(); }
   }

   public bool Remove(T item)
   {
       throw new NotImplementedException();
   }

   public IEnumerator<T> GetEnumerator()
   {
       return InnerList.GetEnumerator();
   }

   System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
   {
       return InnerList.GetEnumerator();
   }
   #endregion

这是派生类:

public class Moshakhase :XMLCollection<Moshakhase>, IDisposable , IComparable<Moshakhase> , IEquatable<Moshakhase>


{
   [XmlAttribute]
   public int Id { get; set; }

   [XmlAttribute]
   public string Name { get; set; }

   #region ctor

   public Moshakhase()
   {

   }

   public Moshakhase(int id, string name)
   {
       this.Id = id;

       this.Name = name;
   }

   #endregion

   public override string ToString()
   {
       return string.Format("{0}:\tId:{1}\tName:{2}",this.GetType().Name ,this.Id,this.Name);
   }




   #region dispose

   public void Dispose()
   {
       GC.SuppressFinalize(this);
   }

   #endregion

   #region IComparable
   /// <summary>
   /// براساس نام
   /// </summary>
   /// <param name="other"></param>
   /// <returns></returns>
   public int CompareTo(Moshakhase other)
   {
       return this.Name.CompareTo(other.Name);
   }

   #endregion


   #region IEquatable

   public bool Equals(Moshakhase other)
   {
       if (this.Id== other.Id)
       {
           return true;
       }

       return false;
   }

   #endregion

}

感谢您的关注。

1 个答案:

答案 0 :(得分:0)

我不知道为什么我从IList工作中移除了XMLCollection<T>。 也许是因为我在其他继承级别实现了IList

注意:在序列化中,实现空白构造函数

非常重要