2using System.Collections.Generic;
15 private readonly Dictionary<string, int> keywordsDictionary;
22 keywordsDictionary =
new Dictionary<string, int>();
31 public void Write(Stream serializationStream, List<SerializationInfo> objectData)
33 this.keywordsDictionary.Clear();
36 writer.Write(objectData.Count);
37 foreach (var serializationInfo
in objectData)
39 writer.Write(serializationInfo.ReferenceId);
40 WriteSystemString(serializationInfo.TypeName, writer);
42 writer.Write(serializationInfo.Children.Count);
43 foreach (var childData
in serializationInfo.Children)
45 WriteSystemString(childData.Key, writer);
46 writer.Write(childData.Value.IsDirty);
47 writer.Write(childData.Value.ReferenceId);
49 writer.Write(serializationInfo.Values.Count);
50 foreach (var valueData
in serializationInfo.Values)
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);
62 private void WriteSystemString(
string systemString, BinaryWriter writer)
64 var checkResult = GetKey(systemString);
65 if (checkResult.IsNew)
68 writer.Write(systemString);
69 writer.Write(checkResult.Key);
74 writer.Write(checkResult.Key);
78 private DictionaryCheckResult GetKey(
string value)
80 DictionaryCheckResult returnValue;
82 if (keywordsDictionary.TryGetValue(value, out var key))
84 returnValue =
new DictionaryCheckResult(
false, key);
88 returnValue =
new DictionaryCheckResult(
true, keywordsDictionary.Count);
89 keywordsDictionary.Add(value, returnValue.Key);
94 private void Write(
object target, BinaryWriter writer)
100 else if (target is IMobileObject)
102 using (MemoryStream buffer =
new MemoryStream())
104 var formatter = SerializationFormatterFactory.GetFormatter();
105 formatter.Serialize(buffer, target);
106 var data = buffer.ToArray();
108 writer.Write(data.Length);
116 else if (target is TimeSpan)
119 writer.Write(((TimeSpan)target).Ticks);
121 else if (target is DateTimeOffset)
124 writer.Write(((DateTimeOffset)target).Ticks);
125 writer.Write(((DateTimeOffset)target).Offset.Ticks);
127 else if (target is
byte[])
130 writer.Write(((
byte[])target).Length);
131 writer.Write((
byte[])target);
133 else if (target is
byte[][] outerArray)
136 writer.Write(outerArray.Length);
137 foreach (var item
in outerArray)
139 writer.Write(item.Length);
143 else if (target is
char[])
146 writer.Write(((
char[])target).Length);
147 writer.Write((
char[])target);
149 else if (target is Guid)
152 writer.Write(((Guid)target).ToByteArray());
154 else if (target is List<int>)
157 writer.Write(((List<int>)target).Count);
158 foreach (var oneInt
in ((List<int>)target))
160 writer.Write(oneInt);
165 var typeCode = Type.GetTypeCode(target.GetType());
168 case TypeCode.Boolean:
170 writer.Write((
bool)target);
174 writer.Write((
char)target);
178 writer.Write((sbyte)target);
182 writer.Write((
byte)target);
186 writer.Write((
short)target);
188 case TypeCode.UInt16:
190 writer.Write((ushort)target);
194 writer.Write((
int)target);
196 case TypeCode.UInt32:
198 writer.Write((uint)target);
202 writer.Write((
long)target);
204 case TypeCode.UInt64:
206 writer.Write((ulong)target);
208 case TypeCode.Single:
210 writer.Write((
float)target);
212 case TypeCode.Double:
214 writer.Write((
double)target);
216 case TypeCode.Decimal:
218 var bits =
Decimal.GetBits((decimal)target);
219 foreach (var bit
in bits)
224 case TypeCode.DateTime:
226 var value = ((
DateTime)target).Ticks;
229 case TypeCode.String:
231 writer.Write((
string)target);
234 throw new NotSupportedException(
235 $
"{Resources.BinaryWriterObjectSerializationException} ({target.GetType().FullName})");
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,...
CslaKnownTypes
This enumeration contains the list of known types that CslaBinaryReader and CslaBinaryWriterknow abou...