CSLA.NET 5.4.2
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
CslaLegacyBinaryWriter.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.IO;
4using Csla.Properties;
5using Csla.Reflection;
6
8{
14 {
15 private readonly Dictionary<string, int> keywordsDictionary;
16
21 {
22 keywordsDictionary = new Dictionary<string, int>();
23 }
24
25
32 public void Write(Stream serializationStream, List<SerializationInfo> objectData)
33 {
34 keywordsDictionary.Clear();
35 using (var writer = new CslaNonClosingBinaryWriter(serializationStream))
36 {
37 writer.Write(objectData.Count);
38 foreach (var serializationInfo in objectData)
39 {
40 writer.Write(serializationInfo.ReferenceId);
41 WriteSystemString(serializationInfo.TypeName, writer);
42
43 writer.Write(serializationInfo.Children.Count);
44 foreach (var childData in serializationInfo.Children)
45 {
46 WriteSystemString(childData.Key, writer);
47 Write(childData.Value.IsDirty, writer);
48 Write(childData.Value.ReferenceId, writer);
49 }
50 writer.Write(serializationInfo.Values.Count);
51 foreach (var valueData in serializationInfo.Values)
52 {
53 WriteSystemString(valueData.Key, writer);
54 WriteSystemString(valueData.Value.EnumTypeName ?? string.Empty, writer);
55 Write(valueData.Value.IsDirty, writer);
56 Write(valueData.Value.Value, writer);
57 }
58 }
59 writer.Flush();
60 }
61 }
62
63 private void WriteSystemString(string systemString, BinaryWriter writer)
64 {
65 var checkResult = GetKey(systemString);
66 if (checkResult.IsNew)
67 {
68 Write(CslaKnownTypes.StringWithDictionaryKey, writer);
69 writer.Write(systemString);
70 writer.Write(checkResult.Key);
71 }
72 else
73 {
74 Write(CslaKnownTypes.StringDictionaryKey, writer);
75 writer.Write(checkResult.Key);
76 }
77 }
78
79 private DictionaryCheckResult GetKey(string value)
80 {
81 DictionaryCheckResult returnValue;
82 int key;
83 if (keywordsDictionary.TryGetValue(value, out key))
84 {
85 returnValue = new DictionaryCheckResult(false, key);
86 }
87 else
88 {
89 returnValue = new DictionaryCheckResult(true, keywordsDictionary.Count);
90 keywordsDictionary.Add(value, returnValue.Key);
91 }
92 return returnValue;
93 }
94
95 private void Write(object target, BinaryWriter writer)
96 {
97 if (target == null)
98 {
99 Write(CslaKnownTypes.Null, writer);
100 }
101 else if (target is CslaKnownTypes)
102 {
103 writer.Write((byte)((CslaKnownTypes)target));
104 }
105 else if (target is TimeSpan)
106 {
107 Write(CslaKnownTypes.TimeSpan, writer);
108 writer.Write(((TimeSpan)target).Ticks);
109 }
110 else if (target is DateTimeOffset)
111 {
112 Write(CslaKnownTypes.DateTimeOffset, writer);
113 writer.Write(((DateTimeOffset)target).Ticks);
114 writer.Write(((DateTimeOffset)target).Offset.Ticks);
115 }
116 else if (target is byte[])
117 {
118 Write(CslaKnownTypes.ByteArray, writer);
119 writer.Write(((byte[])target).Length);
120 writer.Write((byte[])target);
121 }
122 else if (target is byte[][] outerArray)
123 {
124 Write(CslaKnownTypes.ByteArrayArray, writer);
125 writer.Write(outerArray.Length);
126 foreach (var item in outerArray)
127 {
128 writer.Write(item.Length);
129 writer.Write(item);
130 }
131 }
132 else if (target is char[])
133 {
134 Write(CslaKnownTypes.CharArray, writer);
135 writer.Write(((char[])target).Length);
136 writer.Write((char[])target);
137 }
138 else if (target is Guid)
139 {
140 Write(CslaKnownTypes.Guid, writer);
141 writer.Write(((Guid)target).ToByteArray());
142 }
143 else if (target is List<int>)
144 {
145 Write(CslaKnownTypes.ListOfInt, writer);
146 writer.Write(((List<int>)target).Count);
147 foreach (var oneInt in ((List<int>)target))
148 {
149 writer.Write(oneInt);
150 }
151 }
152 else
153 {
154 var typeCode = Type.GetTypeCode(target.GetType());
155 switch (typeCode)
156 {
157 case TypeCode.Boolean:
158 Write(CslaKnownTypes.Boolean, writer);
159 writer.Write((Boolean)target);
160 break;
161 case TypeCode.Char:
162 Write(CslaKnownTypes.Char, writer);
163 writer.Write((Char)target);
164 break;
165 case TypeCode.SByte:
166 Write(CslaKnownTypes.SByte, writer);
167 writer.Write((SByte)target);
168 break;
169 case TypeCode.Byte:
170 Write(CslaKnownTypes.Byte, writer);
171 writer.Write((Byte)target);
172 break;
173 case TypeCode.Int16:
174 Write(CslaKnownTypes.Int16, writer);
175 writer.Write((Int16)target);
176 break;
177 case TypeCode.UInt16:
178 Write(CslaKnownTypes.UInt16, writer);
179 writer.Write((UInt16)target);
180 break;
181 case TypeCode.Int32:
182 Write(CslaKnownTypes.Int32, writer);
183 writer.Write((Int32)target);
184 break;
185 case TypeCode.UInt32:
186 Write(CslaKnownTypes.UInt32, writer);
187 writer.Write((UInt32)target);
188 break;
189 case TypeCode.Int64:
190 Write(CslaKnownTypes.Int64, writer);
191 writer.Write((Int64)target);
192 break;
193 case TypeCode.UInt64:
194 Write(CslaKnownTypes.UInt64, writer);
195 writer.Write((UInt64)target);
196 break;
197 case TypeCode.Single:
198 Write(CslaKnownTypes.Single, writer);
199 writer.Write((Single)target);
200 break;
201 case TypeCode.Double:
202 Write(CslaKnownTypes.Double, writer);
203 writer.Write((Double)target);
204 break;
205 case TypeCode.Decimal:
206 Write(CslaKnownTypes.Decimal, writer);
207 var bits = Decimal.GetBits((decimal)target);
208 writer.Write(bits.Length);
209 foreach (var bit in bits)
210 {
211 writer.Write(bit);
212 }
213 break;
214 case TypeCode.DateTime:
215 Write(CslaKnownTypes.DateTime, writer);
216 var value = ((DateTime)target).Ticks;
217 writer.Write(value);
218 break;
219 case TypeCode.String:
220 Write(CslaKnownTypes.String, writer);
221 writer.Write((String)target);
222 break;
223 default:
224 throw new NotSupportedException(
225 $"{Resources.BinaryWriterObjectSerializationException} ({target.GetType().FullName})");
226 }
227 }
228 }
229 }
230}
This is a legacy version of CslaBinaryWriter.
void Write(Stream serializationStream, List< SerializationInfo > objectData)
Write a list of SerializationInfo objects into stream, typically MemoryStream
CslaLegacyBinaryWriter()
Creates new instance of CslaLegacyBinaryWriter
This class is used to get around the issue in .NET framework, where underlying stream is closed by a ...
Represents a class that can be used to write a list of SerializationInfo objects into a stream,...
Definition: ICslaWriter.cs:12
CslaKnownTypes
This enumeration contains the list of known types that CslaBinaryReader and CslaBinaryWriterknow abou...