public void DeSerializeNow()
        {
            try
            {
                ClassToSerialize c  = new ClassToSerialize();
                FileStream       fs = File.OpenRead("temp.dat");
                BinaryFormatter  b  = new BinaryFormatter();

                //call deserialize method of BinaryFormatter object and cast it back to object of type ClassToSerialize
                c = (ClassToSerialize)b.Deserialize(fs);
                Console.WriteLine("Deserialization successful!");
                Console.WriteLine("Name: " + c.Name);
                Console.WriteLine("Age: " + c.Age);
                Console.WriteLine("Company Name: " + ClassToSerialize.CompanyName);
                Console.WriteLine("AccountNumber: " + c.AccountNumber);
                Console.WriteLine("Company Name: " + c.GetSupportClassString());
                fs.Close();
            }

            catch (Exception e)
            {
                Console.WriteLine("Ooops! Something went wrong");
                Console.WriteLine(e.StackTrace);
            }
        }
        public void SerializeNow()
        {
            try
            {
                ClassToSerialize c = new ClassToSerialize
                {
                    Name          = "bipin",
                    Age           = 26,
                    AccountNumber = "6258730521"
                };
                ClassToSerialize.CompanyName = "xyz";

                //Create an instance of FileStream that will store serialized object
                using (
                    FileStream fs = File.Create("C:\\Users\\Dev Ops\\Documents\\Visual Studio 2019\\Projects\\SerializationAndDeserialization\\Serialization\\temp.txt")
                    )
                {
                    ////Create an instance of BinaryFormatter
                    BinaryFormatter b = new BinaryFormatter();

                    //Call serialize method of the instance passing it stream and object to serialize
                    b.Serialize(fs, c);
                    Console.WriteLine("Serialization successful");
                }
            }

            catch (Exception e)
            {
                Console.WriteLine("Ooops! Something went wrong");
                Console.WriteLine(e.ToString());
            }
        }
Пример #3
0
        public void SerializeNow()
        {
            ClassToSerialize c = new ClassToSerialize();
            File             f = new File("temp.dat");
            Stream           s = f.Open(FileMode.Create);
            BinaryFormatter  b = new BinaryFormatter();

            b.Serialize(s, c);
            s.Close();
        }
Пример #4
0
        public void DeSerializeNow()
        {
            ClassToSerialize c = new ClassToSerialize();
            File             f = new File("temp.dat");
            Stream           s = f.Open(FileMode.Open);
            BinaryFormatter  b = new BinaryFormatter();

            c = (ClassToSerialize)b.Deserialize(s);
            Console.WriteLine(c.name);
            s.Close();
        }