wasSharpNET – Blame information for rev 22

Subversion Repositories:
Rev:
Rev Author Line No. Line
16 office 1 ///////////////////////////////////////////////////////////////////////////
2 // Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 //
3 // Please see: http://www.gnu.org/licenses/gpl.html for legal details, //
4 // rights of fair usage, the disclaimer and warranty conditions. //
5 ///////////////////////////////////////////////////////////////////////////
6 // Based on: Danilow @ https://stackoverflow.com/questions/23897145/memory-leak-using-streamreader-and-xmlserializer/
7  
8 using System;
9 using System.Collections.Generic;
10 using System.IO;
22 office 11 using System.Linq;
16 office 12 using System.Threading;
13 using System.Xml;
14 using System.Xml.Linq;
15 using System.Xml.Serialization;
16  
17 namespace wasSharpNET.Serialization
18 {
19 public static class XmlSerializerCache
20 {
22 office 21 private static readonly ReaderWriterLockSlim SerializerCacheLock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
22 private static readonly Dictionary<string, XmlSerializer> SerializerCache = new Dictionary<string, XmlSerializer>();
16 office 23  
24 public static XmlSerializer GetSerializer<T>()
25 {
26 return GetSerializer<T>(null);
27 }
28  
29 public static XmlSerializer GetSerializer<T>(Type[] ExtraTypes)
30 {
31 return GetSerializer(typeof(T), ExtraTypes);
32 }
33  
34 public static XmlSerializer GetSerializer(Type MainTypeForSerialization)
35 {
36 return GetSerializer(MainTypeForSerialization, null);
37 }
38  
39 public static XmlSerializer GetSerializer(Type MainTypeForSerialization, Type[] ExtraTypes)
40 {
22 office 41 var Signature = MainTypeForSerialization.FullName;
16 office 42 if (ExtraTypes != null)
43 {
22 office 44 Signature = ExtraTypes.Aggregate(Signature, (current, tp) => current + ("-" + tp.FullName));
16 office 45 }
46  
47 SerializerCacheLock.EnterReadLock();
22 office 48 XmlSerializer XmlEventSerializer;
16 office 49 if (SerializerCache.TryGetValue(Signature, out XmlEventSerializer))
50 {
51 SerializerCacheLock.ExitReadLock();
52 return XmlEventSerializer;
53 }
54 SerializerCacheLock.ExitReadLock();
55  
56 if (ExtraTypes == null)
57 return new XmlSerializer(MainTypeForSerialization);
58  
59 XmlEventSerializer = new XmlSerializer(MainTypeForSerialization, ExtraTypes);
60 SerializerCacheLock.EnterWriteLock();
61 SerializerCache.Add(Signature, XmlEventSerializer);
62 SerializerCacheLock.ExitWriteLock();
63  
64 return XmlEventSerializer;
65 }
66  
67 public static T Deserialize<T>(XDocument XmlData)
68 {
69 return Deserialize<T>(XmlData, null);
70 }
71  
72 public static T Deserialize<T>(XDocument XmlData, Type[] ExtraTypes)
73 {
74 try
75 {
76 using (var XmlReader = XmlData.Root.CreateReader())
77 {
22 office 78 return (T) GetSerializer<T>(ExtraTypes).Deserialize(XmlReader);
16 office 79 }
80 }
81 catch (Exception ex)
82 {
83 throw new Exception("Could not deserialize to " + typeof(T).Name, ex);
84 }
85 }
86  
87 public static T Deserialize<T>(string XmlData)
88 {
89 return Deserialize<T>(XmlData, null);
90 }
91  
92 public static T Deserialize<T>(string XmlData, Type[] ExtraTypes)
93 {
94 try
95 {
96 using (MemoryStream memoryStream = new MemoryStream())
97 {
98 using (StreamWriter streamWriter = new StreamWriter(memoryStream))
99 {
100 streamWriter.Write(XmlData);
101 streamWriter.Flush();
102 memoryStream.Position = 0;
103 return (T)GetSerializer<T>(ExtraTypes).Deserialize(memoryStream);
104 }
105 }
106 }
107 catch (Exception ex)
108 {
109 throw new Exception("Could not deserialize to " + typeof(T).Name, ex);
110 }
111 }
112  
113 public static XDocument Serialize<T>(T Object, Type[] ExtraTypes)
114 {
115 try
116 {
117 using (MemoryStream memoryStream = new MemoryStream())
118 {
119 using (StreamReader streamReader = new StreamReader(memoryStream))
120 {
121 using (var xmlWriter = XmlWriter.Create(memoryStream, new XmlWriterSettings { Indent = true }))
122 {
22 office 123 var ns = new XmlSerializerNamespaces();
16 office 124 ns.Add(string.Empty, string.Empty);
125 GetSerializer<T>(ExtraTypes).Serialize(xmlWriter, Object, ns);
126 xmlWriter.Flush();
127 memoryStream.Position = 0L;
128 return XDocument.Load(streamReader, LoadOptions.None);
129 }
130 }
131 }
132 }
133 catch (Exception ex)
134 {
135 throw new Exception("Could not serialize from " + typeof(T).Name + " to xml string", ex);
136 }
137 }
138  
139 public static XDocument Serialize<T>(T Object)
140 {
141 return Serialize(Object, null);
142 }
143  
144 public static T Deserialize<T>(StreamReader streamReader)
145 {
146 return Deserialize<T>(streamReader.ReadToEnd(), null);
147 }
148  
149 public static T Deserialize<T>(Stream stream)
150 {
22 office 151 using (var memoryStream = new MemoryStream())
16 office 152 {
153 stream.CopyTo(memoryStream);
154 memoryStream.Position = 0L;
155 using (var streamReader = new StreamReader(memoryStream))
156 {
157 return Deserialize<T>(streamReader.ReadToEnd(), null);
158 }
159 }
160 }
161  
162 public static void Serialize<T>(Stream stream, T value)
163 {
164 Serialize(value, null).Save(stream);
165 }
166  
167 public static void Serialize<T>(StreamWriter streamWriter, T value)
168 {
169 Serialize(value, null).Save(streamWriter);
170 }
171  
172 public static void Serialize<T>(StringWriter stringWriter, T value)
173 {
174 Serialize(value, null).Save(stringWriter);
175 }
176  
177 public static T Deserialize<T>(TextReader reader)
178 {
179 return Deserialize<T>(reader.ReadToEnd(), null);
180 }
181 }
182 }