9using System.Collections.Generic;
12using System.Runtime.Serialization;
23 [System.Diagnostics.DebuggerStepThrough]
40 public void Serialize(Stream serializationStream,
object graph)
42 ICslaWriter writer = CslaReaderWriterFactory.GetCslaWriter();
52 using var buffer =
new MemoryStream();
55 return buffer.ToArray();
67 _serializationReferences.Clear();
71 ConvertEnumsToIntegers();
72 List<SerializationInfo> serialized = _serializationReferences.Values.ToList();
92 private void ConvertEnumsToIntegers()
98 if (fieldData.Value !=
null)
100 Type fieldType = fieldData.Value.GetType();
102 if (fieldType.IsEnum)
104 fieldData.Value = Convert.ChangeType(fieldData.Value, Enum.GetUnderlyingType(fieldType));
105 fieldData.EnumTypeName = fieldType.AssemblyQualifiedName;
125 _serializationReferences.Add(nullPlaceholder, info);
131 var thisType = obj.GetType();
132 if (obj is System.Security.Claims.ClaimsPrincipal cp)
134 obj =
new Security.CslaClaimsPrincipal(cp);
135 thisType = obj.GetType();
137 if (!thisType.IsSerializable)
138 throw new InvalidOperationException(
141 throw new InvalidOperationException(
145 if (!_serializationReferences.TryGetValue(mobile, out info))
148 _serializationReferences.Add(mobile, info);
150 info.TypeName = AssemblyNameTranslator.GetAssemblyQualifiedName(thisType);
151 if (thisType.Equals(typeof(Security.CslaClaimsPrincipal)))
153 var principal = (Security.CslaClaimsPrincipal)obj;
154 using (var buffer =
new System.IO.MemoryStream())
156 using (var writer =
new System.IO.BinaryWriter(buffer))
158 principal.WriteTo(writer);
159 info.AddValue(
"s", buffer.ToArray());
165 mobile.GetChildren(info,
this);
166 mobile.GetState(info);
173 private Dictionary<IMobileObject, SerializationInfo> _serializationReferences =
180 private Dictionary<int, IMobileObject> _deserializationReferences =
181 new Dictionary<int, IMobileObject>();
183 private Dictionary<string, Type> _typeCache =
new Dictionary<string, Type>();
185 private Type GetTypeFromCache(
string typeName)
188 if (!_typeCache.TryGetValue(typeName, out result))
194 throw new SerializationException(
string.Format(
198 _typeCache.Add(typeName, result);
214 if (serializationStream ==
null)
217 ICslaReader reader = CslaReaderWriterFactory.GetCslaReader();
218 var data = reader.
Read(serializationStream);
232 if (buffer.Length == 0)
234 using var serializationStream =
new MemoryStream(buffer);
235 ICslaReader reader = CslaReaderWriterFactory.GetCslaReader();
236 var data = reader.
Read(serializationStream);
248 _deserializationReferences =
new Dictionary<int, IMobileObject>();
251 var typeName = AssemblyNameTranslator.GetAssemblyQualifiedName(info.
TypeName);
252 Type type = GetTypeFromCache(typeName);
256 throw new SerializationException(
string.Format(
261 _deserializationReferences.Add(info.
ReferenceId,
null);
265 if (type.Equals(typeof(Security.CslaClaimsPrincipal)))
267 var state = info.GetValue<
byte[]>(
"s");
268 using (var buffer =
new System.IO.MemoryStream(state))
270 using (var reader =
new System.IO.BinaryReader(buffer))
273 _deserializationReferences.Add(info.
ReferenceId, mobile);
281 _deserializationReferences.Add(info.
ReferenceId, mobile);
283 ConvertEnumsFromIntegers(info);
302 if (notifiable !=
null)
305 return (_deserializationReferences.Count > 0 ? _deserializationReferences[1] :
null);
313 private static void ConvertEnumsFromIntegers(
SerializationInfo serializationInfo)
317 if (!
String.IsNullOrEmpty(fieldData.EnumTypeName))
319 Type enumType = MethodCaller.GetType(fieldData.EnumTypeName);
320 fieldData.Value = Enum.ToObject(enumType, fieldData.Value);
333 return _deserializationReferences[referenceId];
338#region Static Helpers
350 using (var buffer =
new System.IO.MemoryStream())
353 formatter.Serialize(buffer, obj);
354 return buffer.ToArray();
369 return formatter.SerializeAsDTO(obj);
380 return formatter.DeserializeAsDTO(serialized);
400 using (var buffer =
new System.IO.MemoryStream(data))
403 return formatter.Deserialize(buffer);
425 return formatter.DeserializeAsDTO(data);
A strongly-typed resource class, for looking up localized strings, etc.
static string MobileFormatterUnableToDeserialize
Looks up a localized string similar to The Type '{0}' was unable to be deserialized,...
static string MustImplementIMobileObject
Looks up a localized string similar to Type {0} must implement IMobileObject.
static string ObjectNotSerializableFormatted
Looks up a localized string similar to Object not serializable ({0}).
Placeholder for null child objects.
Implements an equality comparer for IMobileObject that compares the objects only on the basis is the ...
Object that contains information about a single field.
Object containing the serialization data for a specific object.
Dictionary< string, FieldData > Values
Dictionary containg field data.
int ReferenceId
Reference number for this object.
string TypeName
Assembly-qualified type name of the object being serialized.
Represents a reader class that can be used to read the data sent across the wire in byte array format...
List< SerializationInfo > Read(Stream serializationStream)
Read the data from a stream and return a list of SerializationInfo objects
Represents a class that can be used to write a list of SerializationInfo objects into a stream,...
void Write(Stream serializationStream, List< SerializationInfo > objectData)
Write a list of SerializationInfo objects into a stream, typically MemoryStream
Interface to be implemented by any object that supports serialization by the SerializationFormatterFa...
void SetChildren(SerializationInfo info, MobileFormatter formatter)
Method called by MobileFormatter when an object should deserialize its child references.
void SetState(SerializationInfo info)
Method called by MobileFormatter when an object should be deserialized.
Interface defining callback methods used by the SerializationFormatterFactory.GetFormatter().
void Deserialized()
Method called on an object after deserialization is complete.