Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to serialize a class in C#

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 192
    Comment on it

    Hello All,

    Working with ASP.NET C#, I wanted to serialize a class to a file which will be later de Serialize to access its properties and delegates, and to do this we have following block of code :

    Structure of the class to be Serialized :

    [Serializable]
    public class CustomClass
    {
       public myDelegate _myDelegate { get; set; }
       public CustomClass(myDelegate delegateObj)
       {
                _myDelegate = delegateObj;
       }
       private CustomClass()
       {
       }
    }
    

    Here we are creating a class with name "CustomClass" and we have to decorate this class with Serializable, which enable this class to be serialized. This class contains a property which is a object of a delegate. Now to perform Serialization and de-serialization of class to a file we have following block of code :

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.Text;
    using System.Threading.Tasks;
    using System.Xml;
    using System.Xml.Serialization;
    
    namespace TestDeligate
    {   
      [Serializable]
      class Program
      {
          public void add(object obj)
          {
              Console.WriteLine("Test Success");
          }
          static void Main(string[] args)
          {
              Program prg = new Program();
              prg.SerializeClass();
              Console.WriteLine("Class Serialized");
              prg.DeSerializeClass();
              Console.WriteLine("Class Deserialized");
          }
          public void SerializeClass()
          {
             try
             {
                 CustomClass customclass = new CustomClass(add);
                 IFormatter formatter = new BinaryFormatter();
                 Stream stream = new FileStream("MyFile.xml", FileMode.Create, FileAccess.Write, FileShare.None);
                 formatter.Serialize(stream, customclass);
                 stream.Close();
             }
             catch (Exception ex) { Console.WriteLine(ex.Message.ToString()); }
          }
          public void DeSerializeClass()
          {
            try
            {
               CustomClass customclass;
               var path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "//MyFile.txt";
               System.IO.FileStream file = System.IO.File.Create(path);
               FileStream myFileStream = new FileStream(@"C:\Users\Gautam\Documents\Visual
               Studio 2013\Projects\TestDeligate\TestDeligate\bin\Debug\MyFile.xml", FileMode.Open);
               IFormatter formatter = new BinaryFormatter();
               customclass = (CustomClass)formatter.Deserialize(myFileStream);
               customclass._myDelegate(customclass);
            }
            catch (Exception ex){Console.WriteLine(ex.Message.ToString());}
           }
        }
        public delegate void myDelegate(object obj);
        [Serializable]
        public class CustomClass
        {
            public myDelegate _myDelegate { get; set; }
            public CustomClass(myDelegate delegateObj)
            {
                _myDelegate = delegateObj;
            }
            public CustomClass()
            {
            }
        }
    }
    

    Here we are creating a console application using C#, in which we have a class called Program. I Program class, we have Main method were we will be calling our custom method SerializeClass() to serialize the class to a file called MyFile.txt and DeSerializeClass() to de-serialize a class from MyFile.txt file to check whether we can access the delegate of that class and call a function with the help of it.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: