0

Is there a way to serialize/deserialize a whole class without having to specify each object.

If I plan to add many more items, this will become tedious.

For example:

[Serializable()]
    public class Items : ISerializable
    {
        public List<Product> ProdList;
        public List<Employee> EmpList;
        public List<ListProduct> BuyList;
        public List<ListProduct> SellList;
        public List<ListEmployee> EmpHours;

        public Items()
        {
            ProdList = new List<Product>();
            EmpList = new List<Employee>();
            BuyList = new List<ListProduct>();
            SellList = new List<ListProduct>();
            EmpHours = new List<ListEmployee>();
        }

        public Items(SerializationInfo info, StreamingContext ctxt)
        {
            ProdList = (List<Product>)info.GetValue("ProdList", typeof(List<Product>));
            BuyList = (List<ListProduct>)info.GetValue("BuyList", typeof(List<ListProduct>));
            SellList = (List<ListProduct>)info.GetValue("SellList", typeof(List<ListProduct>));
            EmpList = (List<Employee>)info.GetValue("EmpList", typeof(List<Employee>));
            EmpHours = (List<ListEmployee>)info.GetValue("EmpHours", typeof(List<ListEmployee>));
        }

        public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
        {
            info.AddValue("ProdList", ProdList);
            info.AddValue("BuyList", BuyList);
            info.AddValue("SellList", SellList);
            info.AddValue("EmpList", EmpList);
            info.AddValue("EmpHours", EmpHours);
        }
    }
Synaps3
  • 1,597
  • 2
  • 15
  • 23
  • Yes. Use the [Data Contract Serializer](http://msdn.microsoft.com/en-us/library/ms731073.aspx), and specify a binary format, perhaps via [CreateBinaryWriter](http://msdn.microsoft.com/en-us/library/system.xml.xmldictionarywriter.createbinarywriter.aspx). – John Saunders Dec 10 '14 at 02:52

1 Answers1

0

If you don't want to use helper classes like DataContractSerializer and insist on implementing the ISerializable interface you can use reflection to iterate all item properties in this class and set their corresponding values.

M.G.E
  • 371
  • 1
  • 10