Пример #1
0
            /// <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];
            }