9using System.Collections.Generic;
12using System.Runtime.Serialization;
23 [System.Diagnostics.DebuggerStepThrough]
51 public void Serialize(Stream serializationStream,
object graph)
63 using var buffer =
new MemoryStream();
66 return buffer.ToArray();
78 _serializationReferences.Clear();
82 ConvertEnumsToIntegers();
83 List<SerializationInfo> serialized = _serializationReferences.Values.ToList();
103 private void ConvertEnumsToIntegers()
109 if (fieldData.Value !=
null)
111 Type fieldType = fieldData.Value.GetType();
113 if (fieldType.IsEnum)
115 fieldData.Value = Convert.ChangeType(fieldData.Value, Enum.GetUnderlyingType(fieldType));
116 fieldData.EnumTypeName = fieldType.AssemblyQualifiedName;
136 _serializationReferences.Add(nullPlaceholder, info);
142 var thisType = obj.GetType();
143 if (obj is System.Security.Claims.ClaimsPrincipal cp)
145 obj =
new Security.CslaClaimsPrincipal(cp);
146 thisType = obj.GetType();
148 if (!thisType.IsSerializable)
149 throw new InvalidOperationException(
152 throw new InvalidOperationException(
156 if (!_serializationReferences.TryGetValue(mobile, out info))
159 _serializationReferences.Add(mobile, info);
161 info.TypeName = AssemblyNameTranslator.GetAssemblyQualifiedName(thisType);
162 if (thisType.Equals(typeof(Security.CslaClaimsPrincipal)))
164 var principal = (Security.CslaClaimsPrincipal)obj;
165 using (var buffer =
new System.IO.MemoryStream())
167 using (var writer =
new System.IO.BinaryWriter(buffer))
169 principal.WriteTo(writer);
170 info.AddValue(
"s", buffer.ToArray());
176 mobile.GetChildren(info,
this);
177 mobile.GetState(info);
184 private Dictionary<IMobileObject, SerializationInfo> _serializationReferences =
191 private Dictionary<int, IMobileObject> _deserializationReferences =
192 new Dictionary<int, IMobileObject>();
194 private Dictionary<string, Type> _typeCache =
new Dictionary<string, Type>();
196 private Type GetTypeFromCache(
string typeName)
199 if (!_typeCache.TryGetValue(typeName, out result))
205 throw new SerializationException(
string.Format(
209 _typeCache.Add(typeName, result);
225 if (serializationStream ==
null)
229 var data = reader.
Read(serializationStream);
243 if (buffer.Length == 0)
245 using var serializationStream =
new MemoryStream(buffer);
247 var data = reader.
Read(serializationStream);
259 _deserializationReferences =
new Dictionary<int, IMobileObject>();
262 var typeName = AssemblyNameTranslator.GetAssemblyQualifiedName(info.
TypeName);
263 Type type = GetTypeFromCache(typeName);
267 throw new SerializationException(
string.Format(
272 _deserializationReferences.Add(info.
ReferenceId,
null);
276 if (type.Equals(typeof(Security.CslaClaimsPrincipal)))
278 var state = info.GetValue<
byte[]>(
"s");
279 using (var buffer =
new System.IO.MemoryStream(state))
281 using (var reader =
new System.IO.BinaryReader(buffer))
284 _deserializationReferences.Add(info.
ReferenceId, mobile);
292 _deserializationReferences.Add(info.
ReferenceId, mobile);
294 ConvertEnumsFromIntegers(info);
313 if (notifiable !=
null)
316 return (_deserializationReferences.Count > 0 ? _deserializationReferences[1] :
null);
324 private static void ConvertEnumsFromIntegers(
SerializationInfo serializationInfo)
328 if (!
String.IsNullOrEmpty(fieldData.EnumTypeName))
330 Type enumType = MethodCaller.GetType(fieldData.EnumTypeName);
331 fieldData.Value = Enum.ToObject(enumType, fieldData.Value);
344 return _deserializationReferences[referenceId];
361 using (var buffer =
new System.IO.MemoryStream())
364 formatter.Serialize(buffer, obj);
365 return buffer.ToArray();
380 return formatter.SerializeAsDTO(obj);
391 return formatter.DeserializeAsDTO(serialized);
411 using (var buffer =
new System.IO.MemoryStream(data))
414 return formatter.Deserialize(buffer);
436 return formatter.DeserializeAsDTO(data);
Provides consistent context information between the client and server DataPortal objects.
object CreateInstance(Type objectType, params object[] parameters)
Creates an object using Activator.
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.