locked
Problem with the Serialization of a list of objects RRS feed

  • Question

  • hi every one i wander if i can Serialize an intire list of object as single object 

    here is my code 

    [Serializable]
        public class Cof
        {
            private double _a ;
            private double _b ;
            public double a {
                 get{return this._a;}
                set { this._a = value; }
            }
            public double b {
                 get{return this._b;}
                set { this._b = value; }
            }
            public Cof()
            {
                _a = 0;
                _b = 0;
            }
            public Cof(double p1, double p2)
            {
                
                _a = p1;
                _b = p2;
            }
      public class DonneeMeteo : Data
        {
             [MS_AccessFieldName(FieldName = "CofMeteo", binary = true, FieldType = "LONGBINARY")]
            public List<Cof> CofList { get; set; }
    }

    i've created a smale framework that based on Ms_AccessFielsNameInformation it's stores and recover data from data base

    and if the binary is true it uses this chunck of code 

    if (item.Key.binary)
                        {
                            BinaryFormatter writer = new BinaryFormatter();
                            using (MemoryStream stream = new MemoryStream())
                            {
    
                                writer.Serialize(stream, item.Value.GetValue(d, null));
    
                                rst.Fields[item.Key.FieldName].Value = stream.ToArray();
                            }
    
    
                        }

    the item is just a kyevaluePaire<MS_AccessFieldName, System.Reflection.PropertyInfo> 

    Friday, June 3, 2016 8:40 AM

Answers

  • the code was working just fine i just forgot to recover the information from the Data BASE
    • Marked as answer by hessoka Friday, June 3, 2016 10:43 AM
    Friday, June 3, 2016 10:42 AM

All replies

  • see how should work in this tutorial http://www.codeproject.com/Articles/1789/Object-Serialization-using-C

    • Proposed as answer by Jesus Estevez Friday, June 3, 2016 9:19 AM
    Friday, June 3, 2016 9:19 AM
  • Default XML serialsiation can only work with fully public fields or properties with public get&set. Your classes seem to follow that rule.

    All lists will be re-intepreted to a Array during serialsiation (lists are just wrappers around arrays and make no sense outside of a running programm).

    Actually you could repalce the properties with autoimplement properties:

            public double a {
                 get; set;
            }
            public double b {
                 get; set;
            }

    Note that the constructors are actually accessing the backing field directly. That is the worst case scenario for properties. Never access the backing field directly outside of the properties, especially in class code.

    The best way to avoid using the backing field is still not having a (named) backing field.
    Adding a leading underscore to the backing field is only the 2nd best way, as it avoids ambiguity and mistakes.

    Friday, June 3, 2016 9:23 AM
  • Jesus Estevez this tutorial just explain how to serialise a single object , i'm talking about a list of objects
    Friday, June 3, 2016 9:24 AM
  • the code was working just fine i just forgot to recover the information from the Data BASE
    • Marked as answer by hessoka Friday, June 3, 2016 10:43 AM
    Friday, June 3, 2016 10:42 AM
  • be careful wit the performance of serialize have a look this great article https://blogs.msdn.microsoft.com/youssefm/2009/07/10/comparing-the-performance-of-net-serializers/
    Friday, June 3, 2016 11:47 AM