BinarySerializer.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. Stream stream = new FileStream(pathName + @"\" + fileName + @".obj", FileMode.Create);
  66. try
  67. {
  68. using (stream)
  69. {
  70. formatter.Serialize(stream, stuff);
  71. stream.Close();
  72. }
  73. }
  74. catch (SerializationException e)
  75. {
  76. Console.WriteLine(e.Message);
  77. throw;
  78. }
  79. catch (Exception e)
  80. {
  81. Console.WriteLine(e.Message);
  82. throw;
  83. }
  84. }
  85. }
  86. /// <summary>
  87. /// Deserializes to an object the file whose name is same as the full name of the object type.
  88. /// </summary>
  89. /// <returns></returns>
  90. public static T FromStream()
  91. {
  92. lock (thisLock)
  93. {
  94. if (!Directory.Exists(pathName))
  95. {
  96. Directory.CreateDirectory(pathName);
  97. }
  98. T obj = default(T);
  99. var fileName = typeof(T).FullName;
  100. var fullName = pathName + @"\" + fileName + @".obj";
  101. if (!File.Exists(fullName))
  102. {
  103. return new T();
  104. }
  105. IFormatter formatter = new BinaryFormatter();
  106. Stream stream = new FileStream(fullName, FileMode.Open, FileAccess.Read);
  107. try
  108. {
  109. using (stream)
  110. {
  111. obj = (T)formatter.Deserialize(stream);
  112. stream.Close();
  113. }
  114. //ISubscriber subscriber = obj as ISubscriber;
  115. //if (subscriber != null)
  116. //{
  117. // subscriber.Subscribe();
  118. //}
  119. }
  120. catch (SerializationException e)
  121. {
  122. Console.WriteLine(e.Message);
  123. //throw;
  124. }
  125. return obj;
  126. }
  127. }
  128. /// <summary>
  129. /// Deserializes a specified file to an object.
  130. /// </summary>
  131. /// <param name="fileName"></param>
  132. /// <returns></returns>
  133. public static T FromStream(string fileName)
  134. {
  135. lock (thisLock)
  136. {
  137. if (!Directory.Exists(pathName))
  138. {
  139. Directory.CreateDirectory(pathName);
  140. }
  141. T obj = default(T);
  142. var fullName = pathName + @"\" + fileName + @".obj";
  143. if (!File.Exists(fullName))
  144. {
  145. return new T();
  146. }
  147. IFormatter formatter = new BinaryFormatter();
  148. Stream stream = new FileStream(fullName, FileMode.Open, FileAccess.Read);
  149. try
  150. {
  151. using (stream)
  152. {
  153. obj = (T)formatter.Deserialize(stream);
  154. stream.Close();
  155. }
  156. }
  157. catch (SerializationException e)
  158. {
  159. Console.WriteLine(e.Message);
  160. //throw;
  161. }
  162. return obj;
  163. }
  164. }
  165. }
  166. }