BinarySerializer.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 Aitex.Common.Util;
  10. namespace MECF.Framework.Common.Utilities
  11. {
  12. public class BinarySerializer<T> where T : new()
  13. {
  14. private static string pathName = PathManager.GetDirectory("Objects");
  15. private static Object thisLock = new Object();
  16. /// <summary>
  17. /// Serializes an object to the file which name is the full name of the object type.
  18. /// </summary>
  19. /// <param name="stuff"></param>
  20. public static void ToStream(T stuff)
  21. {
  22. lock (thisLock)
  23. {
  24. if (!Directory.Exists(pathName))
  25. {
  26. Directory.CreateDirectory(pathName);
  27. }
  28. var fileName = typeof(T).FullName;
  29. IFormatter formatter = new BinaryFormatter();
  30. Stream stream = new FileStream(pathName + @"\" + fileName + @".obj", FileMode.Create);
  31. try
  32. {
  33. using (stream)
  34. {
  35. formatter.Serialize(stream, stuff);
  36. stream.Close();
  37. }
  38. }
  39. catch (SerializationException e)
  40. {
  41. Console.WriteLine(e.Message);
  42. throw;
  43. }
  44. catch (Exception e)
  45. {
  46. Console.WriteLine(e.Message);
  47. throw;
  48. }
  49. }
  50. }
  51. /// <summary>
  52. /// Serializes an object to a file with specified name.
  53. /// </summary>
  54. /// <param name="stuff"></param>
  55. /// <param name="fileName"></param>
  56. public static void ToStream(T stuff, string fileName)
  57. {
  58. lock (thisLock)
  59. {
  60. if (!Directory.Exists(pathName))
  61. {
  62. Directory.CreateDirectory(pathName);
  63. }
  64. IFormatter formatter = new BinaryFormatter();
  65. try
  66. {
  67. using (Stream stream = new FileStream(pathName + @"\" + fileName + @".obj", FileMode.Create))
  68. {
  69. formatter.Serialize(stream, stuff);
  70. }
  71. }
  72. catch (SerializationException e)
  73. {
  74. Console.WriteLine(e.Message);
  75. throw;
  76. }
  77. catch (Exception e)
  78. {
  79. Console.WriteLine(e.Message);
  80. throw;
  81. }
  82. }
  83. }
  84. /// <summary>
  85. /// Deserializes to an object the file whose name is same as the full name of the object type.
  86. /// </summary>
  87. /// <returns></returns>
  88. public static T FromStream()
  89. {
  90. lock (thisLock)
  91. {
  92. if (!Directory.Exists(pathName))
  93. {
  94. Directory.CreateDirectory(pathName);
  95. }
  96. T obj = default(T);
  97. var fileName = typeof(T).FullName;
  98. var fullName = pathName + @"\" + fileName + @".obj";
  99. if (!File.Exists(fullName))
  100. {
  101. return new T();
  102. }
  103. IFormatter formatter = new BinaryFormatter();
  104. Stream stream = new FileStream(fullName, FileMode.Open, FileAccess.Read);
  105. try
  106. {
  107. using (stream)
  108. {
  109. obj = (T)formatter.Deserialize(stream);
  110. stream.Close();
  111. }
  112. //ISubscriber subscriber = obj as ISubscriber;
  113. //if (subscriber != null)
  114. //{
  115. // subscriber.Subscribe();
  116. //}
  117. }
  118. catch (SerializationException e)
  119. {
  120. Console.WriteLine(e.Message);
  121. //throw;
  122. }
  123. return obj;
  124. }
  125. }
  126. /// <summary>
  127. /// Deserializes a specified file to an object.
  128. /// </summary>
  129. /// <param name="fileName"></param>
  130. /// <returns></returns>
  131. public static T FromStream(string fileName)
  132. {
  133. lock (thisLock)
  134. {
  135. if (!Directory.Exists(pathName))
  136. {
  137. Directory.CreateDirectory(pathName);
  138. }
  139. T obj = default(T);
  140. var fullName = pathName + @"\" + fileName + @".obj";
  141. if (!File.Exists(fullName))
  142. {
  143. return new T();
  144. }
  145. IFormatter formatter = new BinaryFormatter();
  146. Stream stream = new FileStream(fullName, FileMode.Open, FileAccess.Read);
  147. try
  148. {
  149. using (stream)
  150. {
  151. obj = (T)formatter.Deserialize(stream);
  152. stream.Close();
  153. }
  154. }
  155. catch (SerializationException e)
  156. {
  157. Console.WriteLine(e.Message);
  158. //throw;
  159. }
  160. return obj;
  161. }
  162. }
  163. }
  164. }