wasSharpNET – Diff between revs 22 and 27
?pathlinks?
Rev 22 | Rev 27 | |||
---|---|---|---|---|
Line 16... | Line 16... | |||
16 | |
16 | |
|
17 | namespace wasSharpNET.Serialization |
17 | namespace wasSharpNET.Serialization |
|
18 | { |
18 | { |
|
19 | public static class XmlSerializerCache |
19 | public static class XmlSerializerCache |
|
- | 20 | { |
||
20 | { |
21 | private static readonly ReaderWriterLockSlim SerializerCacheLock = |
|
- | 22 | new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion); |
||
21 | private static readonly ReaderWriterLockSlim SerializerCacheLock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion); |
23 | |
|
- | 24 | private static readonly Dictionary<string, XmlSerializer> SerializerCache = |
||
Line 22... | Line 25... | |||
22 | private static readonly Dictionary<string, XmlSerializer> SerializerCache = new Dictionary<string, XmlSerializer>(); |
25 | new Dictionary<string, XmlSerializer>(); |
|
23 | |
26 | |
|
24 | public static XmlSerializer GetSerializer<T>() |
27 | public static XmlSerializer GetSerializer<T>() |
|
25 | { |
28 | { |
|
Line 38... | Line 41... | |||
38 | |
41 | |
|
39 | public static XmlSerializer GetSerializer(Type MainTypeForSerialization, Type[] ExtraTypes) |
42 | public static XmlSerializer GetSerializer(Type MainTypeForSerialization, Type[] ExtraTypes) |
|
40 | { |
43 | { |
|
41 | var Signature = MainTypeForSerialization.FullName; |
44 | var Signature = MainTypeForSerialization.FullName; |
|
42 | if (ExtraTypes != null) |
- | ||
43 | { |
45 | if (ExtraTypes != null) |
|
44 | Signature = ExtraTypes.Aggregate(Signature, (current, tp) => current + ("-" + tp.FullName)); |
- | ||
Line 45... | Line 46... | |||
45 | } |
46 | Signature = ExtraTypes.Aggregate(Signature, (current, tp) => current + "-" + tp.FullName); |
|
46 | |
47 | |
|
47 | SerializerCacheLock.EnterReadLock(); |
48 | SerializerCacheLock.EnterReadLock(); |
|
48 | XmlSerializer XmlEventSerializer; |
49 | XmlSerializer XmlEventSerializer; |
|
Line 91... | Line 92... | |||
91 | |
92 | |
|
92 | public static T Deserialize<T>(string XmlData, Type[] ExtraTypes) |
93 | public static T Deserialize<T>(string XmlData, Type[] ExtraTypes) |
|
93 | { |
94 | { |
|
94 | try |
95 | try |
|
95 | { |
96 | { |
|
96 | using (MemoryStream memoryStream = new MemoryStream()) |
97 | using (var memoryStream = new MemoryStream()) |
|
97 | { |
98 | { |
|
98 | using (StreamWriter streamWriter = new StreamWriter(memoryStream)) |
99 | using (var streamWriter = new StreamWriter(memoryStream)) |
|
99 | { |
100 | { |
|
100 | streamWriter.Write(XmlData); |
101 | streamWriter.Write(XmlData); |
|
101 | streamWriter.Flush(); |
102 | streamWriter.Flush(); |
|
102 | memoryStream.Position = 0; |
103 | memoryStream.Position = 0; |
|
Line 112... | Line 113... | |||
112 | |
113 | |
|
113 | public static XDocument Serialize<T>(T Object, Type[] ExtraTypes) |
114 | public static XDocument Serialize<T>(T Object, Type[] ExtraTypes) |
|
114 | { |
115 | { |
|
115 | try |
116 | try |
|
116 | { |
117 | { |
|
117 | using (MemoryStream memoryStream = new MemoryStream()) |
118 | using (var memoryStream = new MemoryStream()) |
|
118 | { |
119 | { |
|
119 | using (StreamReader streamReader = new StreamReader(memoryStream)) |
120 | using (var streamReader = new StreamReader(memoryStream)) |
|
120 | { |
121 | { |
|
121 | using (var xmlWriter = XmlWriter.Create(memoryStream, new XmlWriterSettings { Indent = true })) |
122 | using (var xmlWriter = XmlWriter.Create(memoryStream, new XmlWriterSettings {Indent = true})) |
|
122 | { |
123 | { |
|
123 | var ns = new XmlSerializerNamespaces(); |
124 | var ns = new XmlSerializerNamespaces(); |