Exemplo n.º 1
0
        private void DehydrateObjects()
        {
            List <PersistenceProperties> dehydrated = new List <PersistenceProperties>();

            // Examine each object
            foreach (KeyValuePair <string, object> obj in _persistentObjects)
            {
                if (obj.Value != null)
                {
                    // Notify object that it is about to be dehydrated
                    if (obj.Value is IPersistent && ApplicationMode == ApplicationMode.Deactivated)
                    {
                        (obj.Value as IPersistent).OnDeactivated();
                    }
                    else if (obj.Value is IPersistent && ApplicationMode == ApplicationMode.Closing)
                    {
                        (obj.Value as IPersistent).OnClosing();
                    }

                    IEnumerable <PropertyInfo> pi = null;

                    pi = obj.Value.GetType().GetProperties();


                    // Examine each property
                    foreach (PropertyInfo p in pi)
                    {
                        // Add any properties with Persist attribute
                        object[] atts = null;

                        atts = p.GetCustomAttributes(typeof(Persist), true);


                        if (atts != null && atts.Length > 0)
                        {
                            PersistMode mode = (atts[0] as Persist).PersistMode;
                            if ((this.ApplicationMode == ApplicationMode.Deactivated &&
                                 (mode == PersistMode.TombstonedOnly || mode == PersistMode.Both)) ||
                                (this.ApplicationMode == ApplicationMode.Closing &&
                                 (mode == PersistMode.ClosedOnly || mode == PersistMode.Both)))
                            {
                                var tsp = new PersistenceProperties(obj.Key, obj.Value.GetType().Name, p.Name, p.GetValue(obj.Value, null));

                                dehydrated.Add(tsp);
                            }
                        }
                    }
                }
            }

            dehydrated.TrimExcess();


            PersistenceStorage.StoreObjectToIsolated <List <PersistenceProperties> >(_persistenceProperties, dehydrated, true);
        }
Exemplo n.º 2
0
        private void Rehydrate(string uniqueName, object o)
        {
            try
            {
                IEnumerable <PropertyInfo> pi = null;

                pi = o.GetType().GetProperties();


                // Examine each property
                foreach (PropertyInfo p in pi)
                {
                    // Add any properties with Persist attribute
                    object[] atts = null;
                    atts = p.GetCustomAttributes(typeof(Persist), true);

                    if (atts != null && atts.Length > 0)
                    {
                        PersistMode mode = (atts[0] as Persist).PersistMode;
                        if (_rehydratedProperties != null &&
                            (this.ApplicationMode == ApplicationMode.Activated &&
                             (mode == PersistMode.TombstonedOnly || mode == PersistMode.Both)) ||
                            (this.ApplicationMode == ApplicationMode.Launching &&
                             (mode == PersistMode.ClosedOnly || mode == PersistMode.Both)))
                        {
                            // Get value
                            PersistenceProperties propertyPersisted = _rehydratedProperties.SingleOrDefault(r => r.UniqueName == uniqueName &&
                                                                                                            r.ClassName == o.GetType().Name&& r.PropertyName == p.Name);

                            if (propertyPersisted != null)
                            {
                                object value = propertyPersisted.Value;

                                Type nullableType = Nullable.GetUnderlyingType(p.PropertyType);
                                if (nullableType != null)
                                {
                                    if (nullableType.IsEnum)
                                    {
                                        value = Enum.ToObject(nullableType, value);
                                    }
                                    else
                                    {
                                        value = (value == null) ? null : Convert.ChangeType(value, nullableType, CultureInfo.CurrentCulture);
                                    }
                                }
                                else if (p.PropertyType.IsGenericType)
                                {
                                    value = Convert.ChangeType(value, p.PropertyType.GetGenericTypeDefinition(), CultureInfo.CurrentCulture);
                                }
                                else
                                {
                                    if (p.PropertyType.IsEnum)
                                    {
                                        value = Enum.ToObject(p.PropertyType, value);
                                    }
                                    else
                                    {
                                        value = Convert.ChangeType(value, p.PropertyType, CultureInfo.CurrentCulture);
                                    }
                                }

                                // Assign value
                                p.SetValue(o, value, null);

                                // remove from _rehydrated properties
                                _rehydratedProperties.Remove(propertyPersisted);
                            }
                        }
                    }
                }

                // Notify object that it has been hydrated
                //if (o is IPersistent)
                //{
                //    if (this.ApplicationMode == ApplicationMode.Activated)
                //    {
                //        (o as IPersistent).OnActivated(false, true);
                //    }
                //    else if (this.ApplicationMode == ApplicationMode.Launching)
                //    {
                //        (o as IPersistent).OnLaunched();
                //    }
                //}
            }
            catch (Exception e)
            {
                Debug.WriteLine("Error setting value on reactivation " + e);
                // If anything has gone wrong notify the object to initialise normally
                //if (o is IPersistent)
                //    (o as IPersistent).OnLaunched();
            }
        }