BinarySerializer.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. public static void ToStream(T stuff)
  17. {
  18. lock (thisLock)
  19. {
  20. if (!Directory.Exists(pathName))
  21. {
  22. Directory.CreateDirectory(pathName);
  23. }
  24. var fileName = typeof(T).FullName;
  25. IFormatter formatter = new BinaryFormatter();
  26. Stream stream = new FileStream(pathName + @"\" + fileName + @".obj", FileMode.Create);
  27. try
  28. {
  29. using (stream)
  30. {
  31. formatter.Serialize(stream, stuff);
  32. stream.Close();
  33. }
  34. }
  35. catch (SerializationException e)
  36. {
  37. Console.WriteLine(e.Message);
  38. throw;
  39. }
  40. catch (Exception e)
  41. {
  42. Console.WriteLine(e.Message);
  43. throw;
  44. }
  45. }
  46. }
  47. public static void ToStream(T stuff, string fileName)
  48. {
  49. lock (thisLock)
  50. {
  51. if (!Directory.Exists(pathName))
  52. {
  53. Directory.CreateDirectory(pathName);
  54. }
  55. IFormatter formatter = new BinaryFormatter();
  56. Stream stream = new FileStream(pathName + @"\" + fileName + @".obj", FileMode.Create);
  57. try
  58. {
  59. using (stream)
  60. {
  61. formatter.Serialize(stream, stuff);
  62. stream.Close();
  63. }
  64. }
  65. catch (SerializationException e)
  66. {
  67. Console.WriteLine(e.Message);
  68. throw;
  69. }
  70. catch (Exception e)
  71. {
  72. Console.WriteLine(e.Message);
  73. throw;
  74. }
  75. }
  76. }
  77. public static T FromStream()
  78. {
  79. lock (thisLock)
  80. {
  81. if (!Directory.Exists(pathName))
  82. {
  83. Directory.CreateDirectory(pathName);
  84. }
  85. T obj = default(T);
  86. var fileName = typeof(T).FullName;
  87. var fullName = pathName + @"\" + fileName + @".obj";
  88. if (!File.Exists(fullName))
  89. {
  90. return new T();
  91. }
  92. IFormatter formatter = new BinaryFormatter();
  93. Stream stream = new FileStream(fullName, FileMode.Open, FileAccess.Read);
  94. try
  95. {
  96. using (stream)
  97. {
  98. obj = (T)formatter.Deserialize(stream);
  99. stream.Close();
  100. }
  101. //ISubscriber subscriber = obj as ISubscriber;
  102. //if (subscriber != null)
  103. //{
  104. // subscriber.Subscribe();
  105. //}
  106. }
  107. catch (SerializationException e)
  108. {
  109. Console.WriteLine(e.Message);
  110. //throw;
  111. }
  112. return obj;
  113. }
  114. }
  115. public static T FromStream(string fileName)
  116. {
  117. lock (thisLock)
  118. {
  119. if (!Directory.Exists(pathName))
  120. {
  121. Directory.CreateDirectory(pathName);
  122. }
  123. T obj = default(T);
  124. var fullName = pathName + @"\" + fileName + @".obj";
  125. if (!File.Exists(fullName))
  126. {
  127. return new T();
  128. }
  129. IFormatter formatter = new BinaryFormatter();
  130. Stream stream = new FileStream(fullName, FileMode.Open, FileAccess.Read);
  131. try
  132. {
  133. using (stream)
  134. {
  135. obj = (T)formatter.Deserialize(stream);
  136. stream.Close();
  137. }
  138. }
  139. catch (SerializationException e)
  140. {
  141. Console.WriteLine(e.Message);
  142. //throw;
  143. }
  144. return obj;
  145. }
  146. }
  147. }
  148. }