wasSharpNET – Diff between revs 16 and 22

Subversion Repositories:
Rev:
Show entire fileIgnore whitespace
Rev 16 Rev 22
Line 6... Line 6...
6 // Based on: Danilow @ https://stackoverflow.com/questions/23897145/memory-leak-using-streamreader-and-xmlserializer/ 6 // Based on: Danilow @ https://stackoverflow.com/questions/23897145/memory-leak-using-streamreader-and-xmlserializer/
Line 7... Line 7...
7   7  
8 using System; 8 using System;
9 using System.Collections.Generic; 9 using System.Collections.Generic;
10 using System.IO; 10 using System.IO;
11 using System.Text; 11 using System.Linq;
12 using System.Threading; 12 using System.Threading;
13 using System.Xml; 13 using System.Xml;
14 using System.Xml.Linq; 14 using System.Xml.Linq;
15 using System.Xml.Serialization; -  
Line 16... Line 15...
16 using wasSharp; 15 using System.Xml.Serialization;
17   16  
18 namespace wasSharpNET.Serialization 17 namespace wasSharpNET.Serialization
19 { 18 {
20 public static class XmlSerializerCache 19 public static class XmlSerializerCache
21 { 20 {
Line 22... Line 21...
22 private static ReaderWriterLockSlim SerializerCacheLock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion); 21 private static readonly ReaderWriterLockSlim SerializerCacheLock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
23 private static Dictionary<string, XmlSerializer> SerializerCache = new Dictionary<string, XmlSerializer>(); 22 private static readonly Dictionary<string, XmlSerializer> SerializerCache = new Dictionary<string, XmlSerializer>();
24   23  
25 public static XmlSerializer GetSerializer<T>() 24 public static XmlSerializer GetSerializer<T>()
Line 37... Line 36...
37 return GetSerializer(MainTypeForSerialization, null); 36 return GetSerializer(MainTypeForSerialization, null);
38 } 37 }
Line 39... Line 38...
39   38  
40 public static XmlSerializer GetSerializer(Type MainTypeForSerialization, Type[] ExtraTypes) 39 public static XmlSerializer GetSerializer(Type MainTypeForSerialization, Type[] ExtraTypes)
41 { 40 {
42 string Signature = MainTypeForSerialization.FullName; 41 var Signature = MainTypeForSerialization.FullName;
43 if (ExtraTypes != null) 42 if (ExtraTypes != null)
44 { -  
45 foreach (Type Tp in ExtraTypes) 43 {
46 Signature += "-" + Tp.FullName; 44 Signature = ExtraTypes.Aggregate(Signature, (current, tp) => current + ("-" + tp.FullName));
Line 47... Line 45...
47 } 45 }
48   46  
49 SerializerCacheLock.EnterReadLock(); 47 SerializerCacheLock.EnterReadLock();
50 XmlSerializer XmlEventSerializer = null; 48 XmlSerializer XmlEventSerializer;
51 if (SerializerCache.TryGetValue(Signature, out XmlEventSerializer)) 49 if (SerializerCache.TryGetValue(Signature, out XmlEventSerializer))
52 { 50 {
53 SerializerCacheLock.ExitReadLock(); 51 SerializerCacheLock.ExitReadLock();
Line 75... Line 73...
75 { 73 {
76 try 74 try
77 { 75 {
78 using (var XmlReader = XmlData.Root.CreateReader()) 76 using (var XmlReader = XmlData.Root.CreateReader())
79 { 77 {
80 return (T)GetSerializer<T>(ExtraTypes).Deserialize(XmlReader); 78 return (T) GetSerializer<T>(ExtraTypes).Deserialize(XmlReader);
81 } 79 }
82 } 80 }
83 catch (Exception ex) 81 catch (Exception ex)
84 { 82 {
85 throw new Exception("Could not deserialize to " + typeof(T).Name, ex); 83 throw new Exception("Could not deserialize to " + typeof(T).Name, ex);
Line 120... Line 118...
120 { 118 {
121 using (StreamReader streamReader = new StreamReader(memoryStream)) 119 using (StreamReader streamReader = new StreamReader(memoryStream))
122 { 120 {
123 using (var xmlWriter = XmlWriter.Create(memoryStream, new XmlWriterSettings { Indent = true })) 121 using (var xmlWriter = XmlWriter.Create(memoryStream, new XmlWriterSettings { Indent = true }))
124 { 122 {
125 XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); 123 var ns = new XmlSerializerNamespaces();
126 ns.Add(string.Empty, string.Empty); 124 ns.Add(string.Empty, string.Empty);
127 GetSerializer<T>(ExtraTypes).Serialize(xmlWriter, Object, ns); 125 GetSerializer<T>(ExtraTypes).Serialize(xmlWriter, Object, ns);
128 xmlWriter.Flush(); 126 xmlWriter.Flush();
129 memoryStream.Position = 0L; 127 memoryStream.Position = 0L;
130 return XDocument.Load(streamReader, LoadOptions.None); 128 return XDocument.Load(streamReader, LoadOptions.None);
Line 148... Line 146...
148 return Deserialize<T>(streamReader.ReadToEnd(), null); 146 return Deserialize<T>(streamReader.ReadToEnd(), null);
149 } 147 }
Line 150... Line 148...
150   148  
151 public static T Deserialize<T>(Stream stream) 149 public static T Deserialize<T>(Stream stream)
152 { 150 {
153 using (MemoryStream memoryStream = new MemoryStream()) 151 using (var memoryStream = new MemoryStream())
154 { 152 {
155 stream.CopyTo(memoryStream); 153 stream.CopyTo(memoryStream);
156 memoryStream.Position = 0L; 154 memoryStream.Position = 0L;
157 using (var streamReader = new StreamReader(memoryStream)) 155 using (var streamReader = new StreamReader(memoryStream))