Serialize.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * ==============================================================================
  3. *
  4. * Filename: Serial
  5. * Description:
  6. *
  7. * Version: 1.0
  8. * Created: 2021/2/25 17:24:56
  9. *
  10. * Author: liu.wenjie
  11. *
  12. * ==============================================================================
  13. */
  14. using Newtonsoft.Json;
  15. using System;
  16. using System.Collections.Generic;
  17. using System.IO;
  18. using System.Linq;
  19. using System.Runtime.Serialization.Formatters.Binary;
  20. using System.Text;
  21. using System.Threading.Tasks;
  22. using System.Xml.Serialization;
  23. namespace CommonMethods
  24. {
  25. public class Serialize
  26. {
  27. public static void XmlSerialize<T>(string objname, T obj)
  28. {
  29. try
  30. {
  31. string filename = objname + ".xml";
  32. if (System.IO.File.Exists(filename))
  33. System.IO.File.Delete(filename);
  34. using (FileStream fileStream = new FileStream(filename, FileMode.Create))
  35. {
  36. // 序列化为xml
  37. XmlSerializer formatter = new XmlSerializer(typeof(T));
  38. formatter.Serialize(fileStream, obj);
  39. fileStream.Close();
  40. }
  41. }
  42. catch (Exception ex)
  43. {
  44. System.Windows.Forms.MessageBox.Show(ex.Message);
  45. }
  46. }
  47. public static T XmlDeserailize<T>(string objname)
  48. {
  49. // System.Runtime.Serialization.IFormatter formatter = new XmlSerializer(typeof(Car));
  50. string filename = objname + ".xml";
  51. T obj;
  52. if (!System.IO.File.Exists(filename))
  53. throw new Exception("对反序列化之前,请先序列化");
  54. //Xml格式反序列化
  55. using (Stream stream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
  56. {
  57. XmlSerializer formatter = new XmlSerializer(typeof(T));
  58. obj = (T)formatter.Deserialize(stream);
  59. stream.Close();
  60. }
  61. return obj;
  62. }
  63. public static void BinarySerialize<T>(string filename, T obj)
  64. {
  65. try
  66. {
  67. //string filename = objname + ".ump";
  68. if (System.IO.File.Exists(filename))
  69. System.IO.File.Delete(filename);
  70. using (FileStream fileStream = new FileStream(filename, FileMode.Create))
  71. {
  72. // 用二进制格式序列化
  73. BinaryFormatter binaryFormatter = new BinaryFormatter();
  74. binaryFormatter.Serialize(fileStream, obj);
  75. fileStream.Close();
  76. }
  77. }
  78. catch (Exception ex)
  79. {
  80. System.Windows.Forms.MessageBox.Show(ex.Message);
  81. }
  82. }
  83. public static T BinaryDeserialize<T>(string filename)
  84. {
  85. System.Runtime.Serialization.IFormatter formatter = new BinaryFormatter();
  86. //二进制格式反序列化
  87. T obj;
  88. //string filename = objname + ".ump";
  89. if (!System.IO.File.Exists(filename))
  90. throw new Exception("在反序列化之前,请先序列化");
  91. using (Stream stream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
  92. {
  93. obj = (T)formatter.Deserialize(stream);
  94. stream.Close();
  95. }
  96. return obj;
  97. }
  98. /// <summary>
  99. /// 把对象序列化并返回相应的字节
  100. /// </summary>
  101. /// <param name="pObj">需要序列化的对象</param>
  102. /// <returns>byte[]</returns>
  103. public static byte[] SerializeObject(object pObj)
  104. {
  105. if (pObj == null)
  106. return null;
  107. System.IO.MemoryStream _memory = new System.IO.MemoryStream();
  108. BinaryFormatter formatter = new BinaryFormatter();
  109. formatter.Serialize(_memory, pObj);
  110. _memory.Position = 0;
  111. byte[] read = new byte[_memory.Length];
  112. _memory.Read(read, 0, read.Length);
  113. _memory.Close();
  114. return read;
  115. }
  116. /// <summary>
  117. /// 把字节反序列化成相应的对象
  118. /// </summary>
  119. /// <param name="pBytes">字节流</param>
  120. /// <returns>object</returns>
  121. public static object DeserializeObject(byte[] pBytes)
  122. {
  123. object _newOjb = null;
  124. if (pBytes == null)
  125. return _newOjb;
  126. System.IO.MemoryStream _memory = new System.IO.MemoryStream(pBytes);
  127. _memory.Position = 0;
  128. BinaryFormatter formatter = new BinaryFormatter();
  129. _newOjb = formatter.Deserialize(_memory);
  130. _memory.Close();
  131. return _newOjb;
  132. }
  133. public static void JsonSerialize<T>(T obj, string filePath)
  134. {
  135. string resultJson = JsonConvert.SerializeObject(obj, Formatting.Indented);
  136. FileInfo fi = new FileInfo(filePath);
  137. if (!fi.Directory.Exists)
  138. {
  139. Directory.CreateDirectory(fi.Directory.FullName);
  140. }
  141. FileOperate.WriteFile(filePath, resultJson);
  142. }
  143. public static T JsonDeserializeObject<T>(string jsonString)
  144. {
  145. T obj;
  146. try
  147. {
  148. obj = JsonConvert.DeserializeObject<T>(FileOperate.ReadFile(jsonString));
  149. return obj;
  150. }
  151. catch (Exception)
  152. {
  153. throw;
  154. }
  155. }
  156. }
  157. }