Mercurial > pub > ImplabNet
annotate Implab/Xml/SerializersPool.cs @ 281:e0916ddc9950 v3 tip
code cleanup and refactoring
author | cin |
---|---|
date | Fri, 01 Jun 2018 21:35:24 +0300 |
parents | 6691aff01de1 |
children |
rev | line source |
---|---|
229 | 1 using Implab.Components; |
2 using System; | |
3 using System.Collections.Generic; | |
4 using System.IO; | |
5 using System.Linq; | |
6 using System.Text; | |
7 using System.Threading.Tasks; | |
8 using System.Xml; | |
9 using System.Xml.Serialization; | |
10 | |
11 namespace Implab.Xml { | |
278
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
229
diff
changeset
|
12 [Obsolete("this class will be removed, use XmlDefaultSerializer")] |
229 | 13 public class SerializersPool<T> : ObjectPool<XmlSerializer> { |
14 | |
15 static readonly SerializersPool<T> _instance = new SerializersPool<T>(); | |
16 | |
17 public static SerializersPool<T> Instance { | |
18 get { return _instance; } | |
19 } | |
20 | |
21 #region implemented abstract members of ObjectPool | |
22 protected override XmlSerializer CreateInstance() { | |
23 return new XmlSerializer(typeof(T)); | |
24 } | |
25 #endregion | |
26 | |
27 public T DeserializeFromString(string data) { | |
28 using (var reader = new StringReader(data)) { | |
29 return Deserialize(reader); | |
30 } | |
31 } | |
32 | |
33 public T Deserialize(TextReader reader) { | |
34 var sr = Allocate(); | |
35 try { | |
36 return (T)sr.Deserialize(reader); | |
37 } finally { | |
38 Release(sr); | |
39 } | |
40 } | |
41 | |
42 public T Deserialize(XmlReader reader) { | |
43 var sr = Allocate(); | |
44 try { | |
45 return (T)sr.Deserialize(reader); | |
46 } finally { | |
47 Release(sr); | |
48 } | |
49 } | |
50 | |
51 public string SerializeAsString(T data) { | |
52 using (var writer = new StringWriter()) { | |
53 Serialize(writer, data); | |
54 return writer.ToString(); | |
55 } | |
56 } | |
57 | |
58 public void Serialize(TextWriter writer, T data) { | |
59 var sr = Allocate(); | |
60 try { | |
61 sr.Serialize(writer, data); | |
62 } finally { | |
63 Release(sr); | |
64 } | |
65 } | |
66 | |
67 public void Serialize(XmlWriter writer, T data) { | |
68 var sr = Allocate(); | |
69 try { | |
70 sr.Serialize(writer, data); | |
71 } finally { | |
72 Release(sr); | |
73 } | |
74 } | |
75 | |
76 } | |
77 } |