Question

I don't know why I have Stackoverflow error when Child class call parent class constractor?!

first of all: sorry for my long code, because some of my friends request for it, so I put it for u!

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

this is derived class:

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

}

Thanks for your attention.

Was it helpful?

Solution

I don't know why I removed IList from XMLCollection<T> worked. maybe because I has implemented IList in an other level of inheritance.

Note: In serializing it's important to implement blank constructor

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top