private static object DeserializeWithoutHeader(BinaryReader reader, Type type) { var propertyMap = PropertyMap.Create(type); var obj = Activator.CreateInstance(type); while (true) { var propertyName = propertyMap.DeserializePropertyName(reader); if (propertyName == null) { break; } var property = type.GetProperty(propertyName); if (property != null) { property.SetValue(obj, DeserializePropertyValue(reader, property.PropertyType, property.GetValue(obj))); } else // possibly a field { var field = type.GetField(propertyName); field.SetValue(obj, DeserializePropertyValue(reader, field.FieldType, field.GetValue(obj))); } } return(obj); }
private static void SerializeWithoutHeader(BinaryWriter writer, IState state, bool full) { if (!full && !state.Dirty) { SerializePropertyValue(writer, (byte)0, false); return; } PropertyMap propertyMap; if (propertyMapCache.ContainsKey(state.GetType())) { propertyMap = propertyMapCache[state.GetType()]; } else { propertyMap = PropertyMap.Create(state.GetType()); propertyMapCache.Add(state.GetType(), propertyMap); } var properties = GetSerializableProperties(state, !full); foreach (var property in properties) { propertyMap.SerializePropertyName(writer, property.Key); object propertyValue; Type propertyType; if (state is IVirtualDataProvider vdp) { // If the property has changed we store 'true' otherwise 'false'. writer.Write(vdp.ChangedVirtualDataMembers.Contains(property.Key)); } if (property.Value) // real property { var propertyInfo = state.GetType().GetProperty(property.Key, propertyFlags); propertyValue = propertyInfo.GetValue(state); propertyType = propertyInfo.PropertyType; } else // field { var fieldInfo = state.GetType().GetField(property.Key, propertyFlags); propertyValue = fieldInfo.GetValue(state); propertyType = fieldInfo.FieldType; } if (propertyValue == null) { SerializePropertyNullValue(writer, propertyType); } else { SerializePropertyValue(writer, propertyValue, full); } } // end of state data marker SerializePropertyValue(writer, (byte)0, false); }
private static void DeserializeWithoutHeaderInto(object targetObject, BinaryReader reader) { var type = targetObject.GetType(); PropertyMap propertyMap; if (propertyMapCache.ContainsKey(type)) { propertyMap = propertyMapCache[type]; } else { propertyMap = PropertyMap.Create(type); propertyMapCache.Add(type, propertyMap); } while (true) { var propertyName = propertyMap.DeserializePropertyName(reader); if (propertyName == null) { break; } // We perform a reset (set value to null) when a virtual data provider // notifies about a virtual type change. bool reset = targetObject is IVirtualDataProvider vdp && reader.ReadBoolean(); var property = type.GetProperty(propertyName, propertyFlags); if (property != null) { if (reset) { property.SetValue(targetObject, null); } DeserializePropertyValueInto(targetObject, reader, property); } else // possibly a field { var field = type.GetField(propertyName, propertyFlags); var value = reset ? null : field.GetValue(targetObject); if (value == null && customTypeCreators.ContainsKey(field.FieldType)) { value = customTypeCreators[field.FieldType]?.Invoke(targetObject); } // The serf state data needs some special care. if (value is Serf.StateData serfStateData) { serfStateData.Update(); } field.SetValue(targetObject, DeserializePropertyValue(reader, field.FieldType, value)); } } }
private static void SerializeWithoutHeader(BinaryWriter writer, State state, bool full) { if (!full && !state.Dirty) { SerializePropertyValue(writer, 0, false); return; } var propertyMap = PropertyMap.Create(state.GetType()); var properties = GetSerializableProperties(state, !full); foreach (var property in properties) { propertyMap.SerializePropertyName(writer, property.Key); object propertyValue; Type propertyType; if (property.Value) // real property { var propertyInfo = state.GetType().GetProperty(property.Key); propertyValue = propertyInfo.GetValue(state); propertyType = propertyInfo.PropertyType; } else // field { var fieldInfo = state.GetType().GetField(property.Key); propertyValue = fieldInfo.GetValue(state); propertyType = fieldInfo.FieldType; } if (propertyValue == null) { SerializePropertyNullValue(writer, propertyType); } else { SerializePropertyValue(writer, propertyValue, full); } } // end of state data marker SerializePropertyValue(writer, 0, false); }