none
Serializable , public fields and encapsulation RRS feed

  • Question

  • I create serializable Ghost Class.

    [Serializable]
    public class GhostDataTable
    {
        public GhostDataTable()
        {
            colNames = new ArrayList();
            colTypes = new ArrayList();
            dataRows = new ArrayList();
        }
        public ArrayList colNames;
        public ArrayList colTypes;  
        public ArrayList dataRows;
    }

    And serialize it.

    private void BinarySerialize(DataTable dt, string outputFile)
    {
        BinaryFormatter bf = new BinaryFormatter();
        StreamWriter swBin = new StreamWriter(outputFile);
     
        // Instantiate and fill the worker class
        GhostDataTable ghost = new GhostDataTable();
        CreateTableGraph(dt, ghost);
     
        // Serialize the object
        bf.Serialize(swBin.BaseStream, ghost);
        swBin.Close();
    }

    But the question is about public fields. I mean public ArrayList colNames;  public ArrayList colTypes;    public ArrayList dataRows;

    1. if it's not a public it will not serialize, so i should set it to public.
    2. But if field become a public it conflict with one OOP conception. i mean encapsulation.


    when it possible to set field access modifier to public ? and do not property fo it ?
    Tuesday, May 6, 2008 1:46 PM

Answers