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
    • 198
    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 :

    1. [Serializable]
    2. public class CustomClass
    3. {
    4. public myDelegate _myDelegate { get; set; }
    5. public CustomClass(myDelegate delegateObj)
    6. {
    7. _myDelegate = delegateObj;
    8. }
    9. private CustomClass()
    10. {
    11. }
    12. }

    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 :

    1. using System;
    2. using System.Collections.Generic;
    3. using System.IO;
    4. using System.Linq;
    5. using System.Runtime.Serialization;
    6. using System.Runtime.Serialization.Formatters.Binary;
    7. using System.Text;
    8. using System.Threading.Tasks;
    9. using System.Xml;
    10. using System.Xml.Serialization;
    11.  
    12. namespace TestDeligate
    13. {
    14. [Serializable]
    15. class Program
    16. {
    17. public void add(object obj)
    18. {
    19. Console.WriteLine("Test Success");
    20. }
    21. static void Main(string[] args)
    22. {
    23. Program prg = new Program();
    24. prg.SerializeClass();
    25. Console.WriteLine("Class Serialized");
    26. prg.DeSerializeClass();
    27. Console.WriteLine("Class Deserialized");
    28. }
    29. public void SerializeClass()
    30. {
    31. try
    32. {
    33. CustomClass customclass = new CustomClass(add);
    34. IFormatter formatter = new BinaryFormatter();
    35. Stream stream = new FileStream("MyFile.xml", FileMode.Create, FileAccess.Write, FileShare.None);
    36. formatter.Serialize(stream, customclass);
    37. stream.Close();
    38. }
    39. catch (Exception ex) { Console.WriteLine(ex.Message.ToString()); }
    40. }
    41. public void DeSerializeClass()
    42. {
    43. try
    44. {
    45. CustomClass customclass;
    46. var path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "//MyFile.txt";
    47. System.IO.FileStream file = System.IO.File.Create(path);
    48. FileStream myFileStream = new FileStream(@"C:\Users\Gautam\Documents\Visual
    49. Studio 2013\Projects\TestDeligate\TestDeligate\bin\Debug\MyFile.xml", FileMode.Open);
    50. IFormatter formatter = new BinaryFormatter();
    51. customclass = (CustomClass)formatter.Deserialize(myFileStream);
    52. customclass._myDelegate(customclass);
    53. }
    54. catch (Exception ex){Console.WriteLine(ex.Message.ToString());}
    55. }
    56. }
    57. public delegate void myDelegate(object obj);
    58. [Serializable]
    59. public class CustomClass
    60. {
    61. public myDelegate _myDelegate { get; set; }
    62. public CustomClass(myDelegate delegateObj)
    63. {
    64. _myDelegate = delegateObj;
    65. }
    66. public CustomClass()
    67. {
    68. }
    69. }
    70. }

    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
Reset Password
Fill out the form below and reset your password: