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.
CslaBinaryWriter.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
31 public void Write(Stream serializationStream, List<SerializationInfo> objectData)
32 {
33 this.keywordsDictionary.Clear();
34 using (var writer = new CslaNonClosingBinaryWriter(serializationStream))
35 {
36 writer.Write(objectData.Count);
37 foreach (var serializationInfo in objectData)
38 {
39 writer.Write(serializationInfo.ReferenceId);
40 WriteSystemString(serializationInfo.TypeName, writer);
41
42 writer.Write(serializationInfo.Children.Count);
43 foreach (var childData in serializationInfo.Children)
44 {
45 WriteSystemString(childData.Key, writer);
46 writer.Write(childData.Value.IsDirty);
47 writer.Write(childData.Value.ReferenceId);
48 }
49 writer.Write(serializationInfo.Values.Count);
50 foreach (var valueData in serializationInfo.Values)
51 {
52 WriteSystemString(valueData.Key, writer);
53 WriteSystemString(valueData.Value.EnumTypeName ?? string.Empty, writer);
54 writer.Write(valueData.Value.IsDirty);
55 Write(valueData.Value.Value, writer);
56 }
57 }
58 writer.Flush();
59 }
60 }
61
62 private void WriteSystemString(string systemString, BinaryWriter writer)
63 {
64 var checkResult = GetKey(systemString);
65 if (checkResult.IsNew)
66 {
67 Write(CslaKnownTypes.StringWithDictionaryKey, writer);
68 writer.Write(systemString);
69 writer.Write(checkResult.Key);
70 }
71 else
72 {
73 Write(CslaKnownTypes.StringDictionaryKey, writer);
74 writer.Write(checkResult.Key);
75 }
76 }
77
78 private DictionaryCheckResult GetKey(string value)
79 {
80 DictionaryCheckResult returnValue;
81
82 if (keywordsDictionary.TryGetValue(value, out var key))
83 {
84 returnValue = new DictionaryCheckResult(false, key);
85 }
86 else
87 {
88 returnValue = new DictionaryCheckResult(true, keywordsDictionary.Count);
89 keywordsDictionary.Add(value, returnValue.Key);
90 }
91 return returnValue;
92 }
93
94 private void Write(object target, BinaryWriter writer)
95 {
96 if (target == null)
97 {
98 Write(CslaKnownTypes.Null, writer);
99 }
100 else if (target is IMobileObject)
101 {
102 using (MemoryStream buffer = new MemoryStream())
103 {
104 var formatter = SerializationFormatterFactory.GetFormatter();
105 formatter.Serialize(buffer, target);
106 var data = buffer.ToArray();
107 Write(CslaKnownTypes.IMobileObject, writer);
108 writer.Write(data.Length);
109 writer.Write(data);
110 }
111 }
112 else if (target is CslaKnownTypes)
113 {
114 writer.Write((byte)((CslaKnownTypes)target));
115 }
116 else if (target is TimeSpan)
117 {
118 Write(CslaKnownTypes.TimeSpan, writer);
119 writer.Write(((TimeSpan)target).Ticks);
120 }
121 else if (target is DateTimeOffset)
122 {
123 Write(CslaKnownTypes.DateTimeOffset, writer);
124 writer.Write(((DateTimeOffset)target).Ticks);
125 writer.Write(((DateTimeOffset)target).Offset.Ticks);
126 }
127 else if (target is byte[])
128 {
129 Write(CslaKnownTypes.ByteArray, writer);
130 writer.Write(((byte[])target).Length);
131 writer.Write((byte[])target);
132 }
133 else if (target is byte[][] outerArray)
134 {
135 Write(CslaKnownTypes.ByteArrayArray, writer);
136 writer.Write(outerArray.Length);
137 foreach (var item in outerArray)
138 {
139 writer.Write(item.Length);
140 writer.Write(item);
141 }
142 }
143 else if (target is char[])
144 {
145 Write(CslaKnownTypes.CharArray, writer);
146 writer.Write(((char[])target).Length);
147 writer.Write((char[])target);
148 }
149 else if (target is Guid)
150 {
151 Write(CslaKnownTypes.Guid, writer);
152 writer.Write(((Guid)target).ToByteArray());
153 }
154 else if (target is List<int>)
155 {
156 Write(CslaKnownTypes.ListOfInt, writer);
157 writer.Write(((List<int>)target).Count);
158 foreach (var oneInt in ((List<int>)target))
159 {
160 writer.Write(oneInt);
161 }
162 }
163 else
164 {
165 var typeCode = Type.GetTypeCode(target.GetType());
166 switch (typeCode)
167 {
168 case TypeCode.Boolean:
169 Write(CslaKnownTypes.Boolean, writer);
170 writer.Write((bool)target);
171 break;
172 case TypeCode.Char:
173 Write(CslaKnownTypes.Char, writer);
174 writer.Write((char)target);
175 break;
176 case TypeCode.SByte:
177 Write(CslaKnownTypes.SByte, writer);
178 writer.Write((sbyte)target);
179 break;
180 case TypeCode.Byte:
181 Write(CslaKnownTypes.Byte, writer);
182 writer.Write((byte)target);
183 break;
184 case TypeCode.Int16:
185 Write(CslaKnownTypes.Int16, writer);
186 writer.Write((short)target);
187 break;
188 case TypeCode.UInt16:
189 Write(CslaKnownTypes.UInt16, writer);
190 writer.Write((ushort)target);
191 break;
192 case TypeCode.Int32:
193 Write(CslaKnownTypes.Int32, writer);
194 writer.Write((int)target);
195 break;
196 case TypeCode.UInt32:
197 Write(CslaKnownTypes.UInt32, writer);
198 writer.Write((uint)target);
199 break;
200 case TypeCode.Int64:
201 Write(CslaKnownTypes.Int64, writer);
202 writer.Write((long)target);
203 break;
204 case TypeCode.UInt64:
205 Write(CslaKnownTypes.UInt64, writer);
206 writer.Write((ulong)target);
207 break;
208 case TypeCode.Single:
209 Write(CslaKnownTypes.Single, writer);
210 writer.Write((float)target);
211 break;
212 case TypeCode.Double:
213 Write(CslaKnownTypes.Double, writer);
214 writer.Write((double)target);
215 break;
216 case TypeCode.Decimal:
217 Write(CslaKnownTypes.Decimal, writer);
218 var bits = Decimal.GetBits((decimal)target);
219 foreach (var bit in bits)
220 {
221 writer.Write(bit);
222 }
223 break;
224 case TypeCode.DateTime:
225 Write(CslaKnownTypes.DateTime, writer);
226 var value = ((DateTime)target).Ticks;
227 writer.Write(value);
228 break;
229 case TypeCode.String:
230 Write(CslaKnownTypes.String, writer);
231 writer.Write((string)target);
232 break;
233 default:
234 throw new NotSupportedException(
235 $"{Resources.BinaryWriterObjectSerializationException} ({target.GetType().FullName})");
236 }
237 }
238 }
239 }
240}
This is a class that is responsible for serializing SerializationInfo objects into a Stream for sendi...
CslaBinaryWriter()
Create new instance of CslaBinaryWriter class
void Write(Stream serializationStream, List< SerializationInfo > objectData)
Write a list of SerializationInfo objects into stream, typically MemoryStream.
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...