Exemplo n.º 1
0
 /// <summary>
 /// Tries to gets the value of this settings key in the given profile, if it exists.
 /// </summary>
 /// <param name="value">The resulting value, if found</param>
 /// <param name="searchInParentProfile">If true, the settings service will look in the parent profile of the given profile if the settings key is not defined into it.</param>
 /// <param name="profile">The profile in which to look for the value. If <c>null</c>, it will look in the <see cref="SettingsGroup.CurrentProfile"/>.</param>
 /// <returns><c>true</c> if the value was found, <c>false</c> otherwise.</returns>
 public bool TryGetValue(out object value, bool searchInParentProfile = true, SettingsProfile profile = null)
 {
     profile = profile ?? Group.CurrentProfile;
     if (profile.GetValue(Name, out value, searchInParentProfile, false))
     {
         return(true);
     }
     value = DefaultValueObject;
     return(false);
 }
Exemplo n.º 2
0
        /// <summary>
        /// Gets the value of this settings key in the given profile.
        /// </summary>
        /// <param name="searchInParentProfile">If true, the settings service will look in the parent profile of the given profile if the settings key is not defined into it.</param>
        /// <param name="profile">The profile in which to look for the value. If <c>null</c>, it will look in the <see cref="SettingsGroup.CurrentProfile"/>.</param>
        /// <param name="createInCurrentProfile">If true, the list will be created in the current profile, from the value of its parent profile.</param>
        /// <returns>The value of this settings key.</returns>
        /// <exception cref="KeyNotFoundException">No value can be found in the given profile matching this settings key.</exception>
        public T GetValue(bool searchInParentProfile = true, SettingsProfile profile = null, bool createInCurrentProfile = false)
        {
            object value;

            profile = profile ?? Group.CurrentProfile;
            if (profile.GetValue(Name, out value, searchInParentProfile, createInCurrentProfile))
            {
                return((T)value);
            }
            throw new KeyNotFoundException("Settings key not found");
        }
Exemplo n.º 3
0
        private void ChangeCurrentProfile(SettingsProfile oldProfile, SettingsProfile newProfile)
        {
            if (oldProfile == null)
            {
                throw new ArgumentNullException("oldProfile");
            }
            if (newProfile == null)
            {
                throw new ArgumentNullException("newProfile");
            }
            currentProfile = newProfile;

            lock (SettingsLock)
            {
                foreach (var key in settingsKeys)
                {
                    object oldValue;
                    oldProfile.GetValue(key.Key, out oldValue, true, false);
                    object newValue;
                    newProfile.GetValue(key.Key, out newValue, true, false);
                    var oldList = oldValue as IList;
                    var newList = newValue as IList;

                    bool isDifferent;
                    if (oldList != null && newList != null)
                    {
                        isDifferent = oldList.Count != newList.Count;
                        for (int i = 0; i < oldList.Count && !isDifferent; ++i)
                        {
                            if (!Equals(oldList[i], newList[i]))
                            {
                                isDifferent = true;
                            }
                        }
                    }
                    else
                    {
                        isDifferent = !Equals(oldValue, newValue);
                    }
                    if (isDifferent)
                    {
                        newProfile.NotifyEntryChanged(key.Key);
                    }
                }
            }

            // Changes have been notified, empty the list of modified settings.
            newProfile.ValidateSettingsChanges();
        }
Exemplo n.º 4
0
        /// <summary>
        /// Tries to gets the value of this settings key in the given profile, if it exists.
        /// </summary>
        /// <param name="value">The resulting value, if found</param>
        /// <param name="searchInParentProfile">If true, the settings service will look in the parent profile of the given profile if the settings key is not defined into it.</param>
        /// <param name="profile">The profile in which to look for the value. If <c>null</c>, it will look in the <see cref="SettingsGroup.CurrentProfile"/>.</param>
        /// <returns><c>true</c> if the value was found, <c>false</c> otherwise.</returns>
        public bool TryGetValue(out T value, bool searchInParentProfile = true, SettingsProfile profile = null)
        {
            object obj;

            profile = profile ?? Group.CurrentProfile;
            if (profile.GetValue(Name, out obj, searchInParentProfile, false))
            {
                try
                {
                    value = (T)obj;
                    return(true);
                }
                catch (Exception e)
                {
                    // The cast exception will fallback to the value unfound result.
                    e.Ignore();
                }
            }
            value = DefaultValue;
            return(false);
        }
Exemplo n.º 5
0
        private void ChangeCurrentProfile(SettingsProfile oldProfile, SettingsProfile newProfile)
        {
            if (oldProfile == null) throw new ArgumentNullException("oldProfile");
            if (newProfile == null) throw new ArgumentNullException("newProfile");
            currentProfile = newProfile;

            foreach (var key in settingsKeys)
            {
                object oldValue;
                oldProfile.GetValue(key.Key, out oldValue, true, false);
                object newValue;
                newProfile.GetValue(key.Key, out newValue, true, false);
                var oldList = oldValue as IList;
                var newList = newValue as IList;

                bool isDifferent;
                if (oldList != null && newList != null)
                {
                    isDifferent = oldList.Count != newList.Count;
                    for (int i = 0; i < oldList.Count && !isDifferent; ++i)
                    {
                        if (!Equals(oldList[i], newList[i]))
                            isDifferent = true;
                    }
                }
                else
                {
                    isDifferent = !Equals(oldValue, newValue);
                }
                if (isDifferent)
                {
                    newProfile.NotifyEntryChanged(key.Key);
                }
            }

            // Changes have been notified, empty the list of modified settings.
            newProfile.ValidateSettingsChanges();
        }
Exemplo n.º 6
0
        /// <summary>
        /// Determines whether the specified profile contains key (without checking parent profiles).
        /// </summary>
        /// <param name="profile">The profile.</param>
        /// <returns></returns>
        public bool ContainsKey(SettingsProfile profile)
        {
            object value;

            return(profile.GetValue(Name, out value, false, false));
        }
Exemplo n.º 7
0
 /// <summary>
 /// Determines whether the specified profile contains key (without checking parent profiles).
 /// </summary>
 /// <param name="profile">The profile.</param>
 /// <returns></returns>
 public bool ContainsKey(SettingsProfile profile)
 {
     object value;
     return profile.GetValue(Name, out value, false, false);
 }
Exemplo n.º 8
0
 /// <summary>
 /// Tries to gets the value of this settings key in the given profile, if it exists.
 /// </summary>
 /// <param name="value">The resulting value, if found</param>
 /// <param name="searchInParentProfile">If true, the settings service will look in the parent profile of the given profile if the settings key is not defined into it.</param>
 /// <param name="profile">The profile in which to look for the value. If <c>null</c>, it will look in the <see cref="SettingsGroup.CurrentProfile"/>.</param>
 /// <returns><c>true</c> if the value was found, <c>false</c> otherwise.</returns>
 public bool TryGetValue(out object value, bool searchInParentProfile = true, SettingsProfile profile = null)
 {
     profile = profile ?? Group.CurrentProfile;
     if (profile.GetValue(Name, out value, searchInParentProfile, false))
     {
         return true;
     }
     value = DefaultValueObject;
     return false;
 }
Exemplo n.º 9
0
 /// <summary>
 /// Gets the value of this settings key in the given profile.
 /// </summary>
 /// <param name="searchInParentProfile">If true, the settings service will look in the parent profile of the given profile if the settings key is not defined into it.</param>
 /// <param name="profile">The profile in which to look for the value. If <c>null</c>, it will look in the <see cref="SettingsGroup.CurrentProfile"/>.</param>
 /// <param name="createInCurrentProfile">If true, the list will be created in the current profile, from the value of its parent profile.</param>
 /// <returns>The value of this settings key.</returns>
 /// <exception cref="KeyNotFoundException">No value can be found in the given profile matching this settings key.</exception>
 public virtual object GetValue(bool searchInParentProfile = true, SettingsProfile profile = null, bool createInCurrentProfile = false)
 {
     object value;
     profile = profile ?? Group.CurrentProfile;
     if (profile.GetValue(Name, out value, searchInParentProfile, createInCurrentProfile))
     {
         return value;
     }
     throw new KeyNotFoundException("Settings key not found");
 }