示例#1
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="SettingsService.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 ?? SettingsService.CurrentProfile;
            if (profile.GetValue(Name, out value, searchInParentProfile, createInCurrentProfile))
            {
                return((T)value);
            }
            throw new KeyNotFoundException("Settings key not found");
        }
示例#2
0
        /// <summary>
        /// Tries to gets the list of values of this settings key in the current profile.
        /// </summary>
        /// <param name="list">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="SettingsService.CurrentProfile"/>.</param>
        /// <returns><c>true</c> if the list was found, <c>false</c> otherwise.</returns>
        public bool TryGetList(out IList <T> list, bool searchInParentProfile = true, SettingsProfile profile = null)
        {
            profile = profile ?? SettingsService.CurrentProfile;
            object value;

            if (profile.GetValue(Name, out value, searchInParentProfile, false))
            {
                list = value as IList <T>;
                return(list != null);
            }
            list = null;
            return(false);
        }
示例#3
0
        /// <summary>
        /// Gets the list of values of this settings key in the current 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="SettingsService.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 IList <T> GetList(bool searchInParentProfile = true, SettingsProfile profile = null, bool createInCurrentProfile = false)
        {
            profile = profile ?? SettingsService.CurrentProfile;
            object value;

            if (profile.GetValue(Name, out value, searchInParentProfile, createInCurrentProfile))
            {
                var list = value as IList <T>;
                if (list != null)
                {
                    return(list);
                }
            }

            throw new KeyNotFoundException("Settings key not found");
        }
示例#4
0
        private static 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();
        }
示例#5
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="SettingsService.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 ?? SettingsService.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 = default(T);
            return(false);
        }