Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi I am assuming in Binary Serialization we can serialize any members irrespective of whether they are private or public or internal or protected, the only condition I am aware is that it should have a default constructor in order serialize and de-serialize. However in my below example we are not able to seriliaze private field salary

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
namespace BinarySerilization
{
    [Serializable]
    class Employee
    {

        public int Empid { get; set; }
        public String Empname { get; set; }
        public String Emplocation { get; set; }
        private int Empsalary;

        public int EmpSalary { get; set; }

    }



    class Program
    {
        static void Main(string[] args)
        {

            Employee emp = new Employee(); { emp.Empid = 101; emp.Empname = "xyz"; emp.Emplocation = "Bangalore"; emp.Empsalary = 50000; };
            IFormatter formattter = new BinaryFormatter();
            Stream str = new FileStream(@"C:\Users\XXXX_XX\Documents\Testing\new.txt", FileMode.Open, FileAccess.Write);
            formattter.Serialize(str, emp);
            str.Close();


            ////De-Serlization
            //IFormatter formatter = new BinaryFormatter();
            //Stream str = new FileStream(@"C:\Users\XXXX_XX\Documents\Testing\new.txt", FileMode.Open, FileAccess.Read);
            //Employee emp = formatter.Deserialize(str) as Employee;
            //Console.WriteLine(emp.Empid+"\t"+emp.Empname+"\t"+emp.Emplocation+"\t"+emp.Empsalary);

        }
    }
}
Posted
Updated 29-May-14 3:45am
v3
Comments
[no name] 29-May-14 9:53am    
Probably because it's private... your code won't compile. Did you maybe mean emp.EmpSalary?
ShaHam11 29-May-14 9:56am    
yeah if i include emp.EmpSalary that works but again the private field Empsalary is unused. I wanted to use the private feild
[no name] 29-May-14 9:59am    
Okay so use it. You do know what "private" means don't you?
ShaHam11 29-May-14 10:05am    
Yes I am aware private field is only accessible with in the class correct me if I am wrong(I am just learning concepts) so mean to say we cant do a binary serialization for private feild?
[no name] 29-May-14 10:13am    
Yes you can. You have to use it before you can see it in the serialization process first! And you can't use it like you are trying to do because it's private.

1 solution

Class Employee doesn't access the backing field at all -

C#
class Employee
{

    public int Empid { get; set; }
    public String Empname { get; set; }
    public String Emplocation { get; set; }
    private int Empsalary;

    //This has no ties to the private field - the compiler will generate a (separate) backing field for it
    //public int EmpSalary { get; set; } 

   //This actually uses the backing field 
   public int EmpSalary 
   { 
      get {return Empsalary; }
      set { Empsalary = value;}
   } 
}


OT: Also it calling the field Empsalary is confusing, especially with the lead capital. Personally I'd use _empSalary, though many people would suggest empSalary
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900