/// <summary> /// Restores the backup to the object. /// </summary> public void RestoreBackup() { Dictionary <string, object> oldPropertyValues = null; using (var stream = new MemoryStream(_propertyValuesBackup)) { try { var serializer = SerializationFactory.GetXmlSerializer(); var properties = serializer.DeserializeMembers(_object.GetType(), stream); oldPropertyValues = properties.ToDictionary(property => property.Name, property => property.Value); } catch (Exception ex) { Log.Error(ex, "Failed to deserialize the data for backup, which is weird. However, for Silverlight, Windows Phone and Windows 8 there is no other option"); } } if (oldPropertyValues == null) { return; } foreach (KeyValuePair <string, object> propertyValue in oldPropertyValues) { if (PropertyDataManager.IsPropertyRegistered(_object.GetType(), propertyValue.Key)) { // Set value so the PropertyChanged event is invoked _object.SetValue(propertyValue.Key, propertyValue.Value); } } _object.IsDirty = (bool)_objectValuesBackup[IsDirty]; }
/// <summary> /// Restores the backup to the object. /// </summary> public void RestoreBackup() { Dictionary <string, object> oldPropertyValues = null; using (var stream = new MemoryStream(_propertyValuesBackup)) { #if NET var serializer = SerializationHelper.GetBinarySerializer(false); try { var deserializedStream = serializer.Deserialize(stream); oldPropertyValues = _object.ConvertListToDictionary((List <PropertyValue>)deserializedStream); } catch (Exception ex) { Log.Warning(ex, "Failed to deserialize the data for backup, which is weird. Trying with redirects enabled"); stream.Position = 0L; var binaryFormatterWithRedirects = SerializationHelper.GetBinarySerializer(true); var deserializedStream = binaryFormatterWithRedirects.Deserialize(stream); oldPropertyValues = _object.ConvertListToDictionary((List <PropertyValue>)deserializedStream); } #else // Xml backup, create serializer without using the cache since the dictionary is used for every object, and // we need a "this" object specific dictionary. var serializer = SerializationHelper.GetDataContractSerializer(GetType(), typeof(List <PropertyValue>), "backup", _knownTypesForDeserialization, false); try { var deserializedStream = serializer.ReadObject(stream); oldPropertyValues = _object.ConvertListToDictionary((List <PropertyValue>)deserializedStream); } catch (Exception ex) { Log.Error(ex, "Failed to deserialize the data for backup, which is weird. However, for Silverlight, WP7 and WinRT there is no other option"); } #endif } foreach (KeyValuePair <string, object> propertyValue in oldPropertyValues) { // Set value so the PropertyChanged event is invoked _object.SetValue(propertyValue.Key, propertyValue.Value); } _object.IsDirty = (bool)_objectValuesBackup[IsDirty]; }
/// <summary> /// Restores the backup to the object. /// </summary> public void RestoreBackup() { Dictionary <string, object> oldPropertyValues = null; using (var stream = new MemoryStream(_propertyValuesBackup)) { try { var properties = new List <MemberValue>(); if (_serializer != null) { properties = _serializer.DeserializeMembers(_object.GetType(), stream, null); } oldPropertyValues = properties.ToDictionary(property => property.Name, property => property.Value); } catch (Exception ex) { Log.Error(ex, "Failed to deserialize the data for backup, which is weird"); } } if (oldPropertyValues == null) { return; } foreach (var propertyValue in oldPropertyValues) { if (PropertyDataManager.IsPropertyRegistered(_object.GetType(), propertyValue.Key)) { // Set value so the PropertyChanged event is invoked _object.SetValue(propertyValue.Key, propertyValue.Value); } } _object.IsDirty = (bool)_objectValuesBackup[nameof(ModelBase.IsDirty)]; }