SerializationHelper.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using System;
  2. using System.IO;
  3. using System.Xml.Serialization;
  4. using System.Xml;
  5. using System.Collections.Generic;
  6. using System.Text;
  7. namespace CB.Cache.MemCached
  8. {
  9. /// <summary>
  10. /// 序列化配置文件..
  11. /// </summary>
  12. public class SerializationHelper
  13. {
  14. private SerializationHelper()
  15. { }
  16. private static Dictionary<int, XmlSerializer> serializer_dict = new Dictionary<int, XmlSerializer>();
  17. public static XmlSerializer GetSerializer(Type t)
  18. {
  19. int type_hash = t.GetHashCode();
  20. if (!serializer_dict.ContainsKey(type_hash))
  21. serializer_dict.Add(type_hash, new XmlSerializer(t));
  22. return serializer_dict[type_hash];
  23. }
  24. /// <summary>
  25. /// 反序列化
  26. /// </summary>
  27. /// <param name="type">对象类型</param>
  28. /// <param name="filename">文件路径</param>
  29. /// <returns></returns>
  30. public static object Load(Type type, string filename)
  31. {
  32. FileStream fs = null;
  33. try
  34. {
  35. // open the stream...
  36. fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
  37. XmlSerializer serializer = new XmlSerializer(type);
  38. return serializer.Deserialize(fs);
  39. }
  40. catch (Exception ex)
  41. {
  42. throw ex;
  43. }
  44. finally
  45. {
  46. if (fs != null)
  47. fs.Close();
  48. }
  49. }
  50. /// <summary>
  51. /// 序列化
  52. /// </summary>
  53. /// <param name="obj">对象</param>
  54. /// <param name="filename">文件路径</param>
  55. public static bool Save(object obj, string filename)
  56. {
  57. bool success = false;
  58. FileStream fs = null;
  59. // serialize it...
  60. try
  61. {
  62. fs = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
  63. XmlSerializer serializer = new XmlSerializer(obj.GetType());
  64. serializer.Serialize(fs, obj);
  65. success = true;
  66. }
  67. catch (Exception ex)
  68. {
  69. throw ex;
  70. }
  71. finally
  72. {
  73. if (fs != null)
  74. fs.Close();
  75. }
  76. return success;
  77. }
  78. /// <summary>
  79. /// xml序列化成字符串
  80. /// </summary>
  81. /// <param name="obj">对象</param>
  82. /// <returns>xml字符串</returns>
  83. public static string Serialize(object obj)
  84. {
  85. string returnStr = "";
  86. XmlSerializer serializer = GetSerializer(obj.GetType());
  87. MemoryStream ms = new MemoryStream();
  88. XmlTextWriter xtw = null;
  89. StreamReader sr = null;
  90. try
  91. {
  92. xtw = new System.Xml.XmlTextWriter(ms, Encoding.UTF8);
  93. xtw.Formatting = System.Xml.Formatting.Indented;
  94. serializer.Serialize(xtw, obj);
  95. ms.Seek(0, SeekOrigin.Begin);
  96. sr = new StreamReader(ms);
  97. returnStr = sr.ReadToEnd();
  98. }
  99. catch (Exception ex)
  100. {
  101. throw ex;
  102. }
  103. finally
  104. {
  105. if (xtw != null)
  106. xtw.Close();
  107. if (sr != null)
  108. sr.Close();
  109. ms.Close();
  110. }
  111. return returnStr;
  112. }
  113. public static object DeSerialize(Type type, string s)
  114. {
  115. byte[] b = System.Text.Encoding.UTF8.GetBytes(s);
  116. try
  117. {
  118. XmlSerializer serializer = GetSerializer(type);
  119. return serializer.Deserialize(new MemoryStream(b));
  120. }
  121. catch (Exception ex)
  122. {
  123. throw ex;
  124. }
  125. }
  126. }
  127. }