2using System.Collections.Generic;
15 private readonly Dictionary<string, int> keywordsDictionary;
22 keywordsDictionary =
new Dictionary<string, int>();
32 public void Write(Stream serializationStream, List<SerializationInfo> objectData)
34 keywordsDictionary.Clear();
37 writer.Write(objectData.Count);
38 foreach (var serializationInfo
in objectData)
40 writer.Write(serializationInfo.ReferenceId);
41 WriteSystemString(serializationInfo.TypeName, writer);
43 writer.Write(serializationInfo.Children.Count);
44 foreach (var childData
in serializationInfo.Children)
46 WriteSystemString(childData.Key, writer);
47 Write(childData.Value.IsDirty, writer);
48 Write(childData.Value.ReferenceId, writer);
50 writer.Write(serializationInfo.Values.Count);
51 foreach (var valueData
in serializationInfo.Values)
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);
63 private void WriteSystemString(
string systemString, BinaryWriter writer)
65 var checkResult = GetKey(systemString);
66 if (checkResult.IsNew)
69 writer.Write(systemString);
70 writer.Write(checkResult.Key);
75 writer.Write(checkResult.Key);
79 private DictionaryCheckResult GetKey(
string value)
81 DictionaryCheckResult returnValue;
83 if (keywordsDictionary.TryGetValue(value, out key))
85 returnValue =
new DictionaryCheckResult(
false, key);
89 returnValue =
new DictionaryCheckResult(
true, keywordsDictionary.Count);
90 keywordsDictionary.Add(value, returnValue.Key);
95 private void Write(
object target, BinaryWriter writer)
105 else if (target is TimeSpan)
108 writer.Write(((TimeSpan)target).Ticks);
110 else if (target is DateTimeOffset)
113 writer.Write(((DateTimeOffset)target).Ticks);
114 writer.Write(((DateTimeOffset)target).Offset.Ticks);
116 else if (target is
byte[])
119 writer.Write(((
byte[])target).Length);
120 writer.Write((
byte[])target);
122 else if (target is
byte[][] outerArray)
125 writer.Write(outerArray.Length);
126 foreach (var item
in outerArray)
128 writer.Write(item.Length);
132 else if (target is
char[])
135 writer.Write(((
char[])target).Length);
136 writer.Write((
char[])target);
138 else if (target is Guid)
141 writer.Write(((Guid)target).ToByteArray());
143 else if (target is List<int>)
146 writer.Write(((List<int>)target).Count);
147 foreach (var oneInt
in ((List<int>)target))
149 writer.Write(oneInt);
154 var typeCode = Type.GetTypeCode(target.GetType());
157 case TypeCode.Boolean:
159 writer.Write((Boolean)target);
163 writer.Write((Char)target);
167 writer.Write((SByte)target);
171 writer.Write((Byte)target);
175 writer.Write((Int16)target);
177 case TypeCode.UInt16:
179 writer.Write((UInt16)target);
183 writer.Write((Int32)target);
185 case TypeCode.UInt32:
187 writer.Write((UInt32)target);
191 writer.Write((Int64)target);
193 case TypeCode.UInt64:
195 writer.Write((UInt64)target);
197 case TypeCode.Single:
199 writer.Write((Single)target);
201 case TypeCode.Double:
203 writer.Write((Double)target);
205 case TypeCode.Decimal:
207 var bits =
Decimal.GetBits((decimal)target);
208 writer.Write(bits.Length);
209 foreach (var bit
in bits)
214 case TypeCode.DateTime:
216 var value = ((
DateTime)target).Ticks;
219 case TypeCode.String:
221 writer.Write((String)target);
224 throw new NotSupportedException(
225 $
"{Resources.BinaryWriterObjectSerializationException} ({target.GetType().FullName})");
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,...
CslaKnownTypes
This enumeration contains the list of known types that CslaBinaryReader and CslaBinaryWriterknow abou...