none
is it possible to serialize a class having private data members RRS feed

  • Question

  • is it possible to serialize a class having private/protected data members?
     If possible can some one provide code ?
    Thursday, October 9, 2008 9:49 PM

Answers

  • This happens automatically.  Fields marked with the NonSerializedAttribute, however, will be excluded (http://msdn.microsoft.com/en-us/library/system.nonserializedattribute.aspx).

    The following demonstrates:

        [Serializable]  
        public class Test  
        {  
            private string m_PrivateField;  
     
            public String Data  
            {  
                get 
                {  
                    return m_PrivateField;  
                }  
                set 
                {  
                    m_PrivateField = value;  
                }  
            }  
        } 


            private void PerformTest()  
            {  
                Test t = new Test();  
                t.Data = "Hello, world";  
                MemoryStream ms = new MemoryStream();  
                BinaryFormatter bf = new BinaryFormatter();  
                bf.Serialize(ms, t);  
                ms.Position = 0;  
                Test t2 = (Test)bf.Deserialize(ms);  
                Console.WriteLine(t2.Data); // Prints "Hello, world"  
            } 



    • Marked as answer by Zhi-Xin Ye Thursday, October 16, 2008 5:44 AM
    Thursday, October 9, 2008 10:45 PM

  • You can inherit your class with ISerializable interface and override GetObjectData

    In GetObejctData, add you private members to the serilizationInfo object. This works with XMLSerilization also.

    for code example refer the URL
    http://msdn.microsoft.com/en-us/library/system.runtime.serialization.iserializable.getobjectdata.aspx

    • Edited by BRSr Friday, October 10, 2008 7:30 AM
    • Marked as answer by Zhi-Xin Ye Thursday, October 16, 2008 5:44 AM
    Friday, October 10, 2008 7:27 AM

All replies

  • This happens automatically.  Fields marked with the NonSerializedAttribute, however, will be excluded (http://msdn.microsoft.com/en-us/library/system.nonserializedattribute.aspx).

    The following demonstrates:

        [Serializable]  
        public class Test  
        {  
            private string m_PrivateField;  
     
            public String Data  
            {  
                get 
                {  
                    return m_PrivateField;  
                }  
                set 
                {  
                    m_PrivateField = value;  
                }  
            }  
        } 


            private void PerformTest()  
            {  
                Test t = new Test();  
                t.Data = "Hello, world";  
                MemoryStream ms = new MemoryStream();  
                BinaryFormatter bf = new BinaryFormatter();  
                bf.Serialize(ms, t);  
                ms.Position = 0;  
                Test t2 = (Test)bf.Deserialize(ms);  
                Console.WriteLine(t2.Data); // Prints "Hello, world"  
            } 



    • Marked as answer by Zhi-Xin Ye Thursday, October 16, 2008 5:44 AM
    Thursday, October 9, 2008 10:45 PM
  • Binary serialization automatically saves private fields.  XML serialization only saves public properties.
    Hans Passant.
    Thursday, October 9, 2008 11:35 PM
    Moderator

  • You can inherit your class with ISerializable interface and override GetObjectData

    In GetObejctData, add you private members to the serilizationInfo object. This works with XMLSerilization also.

    for code example refer the URL
    http://msdn.microsoft.com/en-us/library/system.runtime.serialization.iserializable.getobjectdata.aspx

    • Edited by BRSr Friday, October 10, 2008 7:30 AM
    • Marked as answer by Zhi-Xin Ye Thursday, October 16, 2008 5:44 AM
    Friday, October 10, 2008 7:27 AM