public override void Serialize(ref ParameterCollectionData obj, ArchiveMode mode, SerializationStream stream) { if (mode == ArchiveMode.Deserialize) { // Should be null if it was int count = stream.ReadInt32(); for (int i = 0; i < count; ++i) { ParameterKey key = null; object value = null; bool matchingType = false; stream.SerializeExtended(ref key, mode); stream.Serialize(ref matchingType); var valueType = matchingType ? key.PropertyType : typeof(object); if (reuseReferences) { MemberReuseSerializer.SerializeExtended(stream, valueType, ref value, mode); } else { MemberNonSealedSerializer.SerializeExtended(stream, valueType, ref value, mode); } obj.Add(key, value); } } else if (mode == ArchiveMode.Serialize) { stream.Write(obj.Count); foreach (var item in obj) { var key = item.Key; // When serializing convert the value type to the expecting type // This should probably better done at the source (when creating/filling the ParameterCollectionData) var value = item.Key.ConvertValue(item.Value); stream.SerializeExtended(ref key, mode); bool matchingType = value.GetType().GetTypeInfo().IsAssignableFrom(key.PropertyType.GetTypeInfo()); stream.Serialize(ref matchingType); var valueType = matchingType ? key.PropertyType : typeof(object); if (reuseReferences) { MemberReuseSerializer.SerializeExtended(stream, valueType, ref value, mode); } else { MemberNonSealedSerializer.SerializeExtended(stream, valueType, ref value, mode); } } } }
public static void SerializeExtended <T>([NotNull] this SerializationStream stream, ref T obj, ArchiveMode mode, DataSerializer <T> dataSerializer = null) { MemberReuseSerializer <T> .SerializeExtended(ref obj, mode, stream, dataSerializer); }