2using System.Collections.Generic;
15 private readonly Dictionary<string, int> keywordsDictionary;
25 keywordsDictionary =
new Dictionary<string, int>();
34 public void Write(Stream serializationStream, List<SerializationInfo> objectData)
36 this.keywordsDictionary.Clear();
39 writer.Write(objectData.Count);
40 foreach (var serializationInfo
in objectData)
42 writer.Write(serializationInfo.ReferenceId);
43 WriteSystemString(serializationInfo.TypeName, writer);
45 writer.Write(serializationInfo.Children.Count);
46 foreach (var childData
in serializationInfo.Children)
48 WriteSystemString(childData.Key, writer);
49 writer.Write(childData.Value.IsDirty);
50 writer.Write(childData.Value.ReferenceId);
52 writer.Write(serializationInfo.Values.Count);
53 foreach (var valueData
in serializationInfo.Values)
55 WriteSystemString(valueData.Key, writer);
56 WriteSystemString(valueData.Value.EnumTypeName ??
string.Empty, writer);
57 writer.Write(valueData.Value.IsDirty);
58 Write(valueData.Value.Value, writer);
65 private void WriteSystemString(
string systemString, BinaryWriter writer)
67 var checkResult = GetKey(systemString);
68 if (checkResult.IsNew)
71 writer.Write(systemString);
72 writer.Write(checkResult.Key);
77 writer.Write(checkResult.Key);
81 private DictionaryCheckResult GetKey(
string value)
83 DictionaryCheckResult returnValue;
85 if (keywordsDictionary.TryGetValue(value, out var key))
87 returnValue =
new DictionaryCheckResult(
false, key);
91 returnValue =
new DictionaryCheckResult(
true, keywordsDictionary.Count);
92 keywordsDictionary.Add(value, returnValue.Key);
97 private void Write(
object target, BinaryWriter writer)
103 else if (target is IMobileObject)
105 using (MemoryStream buffer =
new MemoryStream())
107 var formatter = SerializationFormatterFactory.GetFormatter(ApplicationContext);
108 formatter.Serialize(buffer, target);
109 var data = buffer.ToArray();
111 writer.Write(data.Length);
119 else if (target is TimeSpan)
122 writer.Write(((TimeSpan)target).Ticks);
124 else if (target is DateTimeOffset)
127 writer.Write(((DateTimeOffset)target).Ticks);
128 writer.Write(((DateTimeOffset)target).Offset.Ticks);
130 else if (target is
byte[])
133 writer.Write(((
byte[])target).Length);
134 writer.Write((
byte[])target);
136 else if (target is
byte[][] outerArray)
139 writer.Write(outerArray.Length);
140 foreach (var item
in outerArray)
142 writer.Write(item.Length);
146 else if (target is
char[])
149 writer.Write(((
char[])target).Length);
150 writer.Write((
char[])target);
152 else if (target is Guid)
155 writer.Write(((Guid)target).ToByteArray());
157 else if (target is List<int>)
160 writer.Write(((List<int>)target).Count);
161 foreach (var oneInt
in ((List<int>)target))
163 writer.Write(oneInt);
168 var typeCode = Type.GetTypeCode(target.GetType());
171 case TypeCode.Boolean:
173 writer.Write((
bool)target);
177 writer.Write((
char)target);
181 writer.Write((sbyte)target);
185 writer.Write((
byte)target);
189 writer.Write((
short)target);
191 case TypeCode.UInt16:
193 writer.Write((ushort)target);
197 writer.Write((
int)target);
199 case TypeCode.UInt32:
201 writer.Write((uint)target);
205 writer.Write((
long)target);
207 case TypeCode.UInt64:
209 writer.Write((ulong)target);
211 case TypeCode.Single:
213 writer.Write((
float)target);
215 case TypeCode.Double:
217 writer.Write((
double)target);
219 case TypeCode.Decimal:
221 var bits =
Decimal.GetBits((decimal)target);
222 foreach (var bit
in bits)
227 case TypeCode.DateTime:
229 var value = ((
DateTime)target).Ticks;
232 case TypeCode.String:
234 writer.Write((
string)target);
237 throw new NotSupportedException(
238 $
"{Resources.BinaryWriterObjectSerializationException} ({target.GetType().FullName})");
Provides consistent context information between the client and server DataPortal objects.
This is a class that is responsible for serializing SerializationInfo objects into a Stream for sendi...
CslaBinaryWriter(ApplicationContext applicationContext)
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...