Esempio n. 1
0
        /// <summary>
        /// Gets value of specified key.
        /// </summary>
        /// <typeparam name="T">Type of value.</typeparam>
        /// <param name="section">A string specifying the section.</param>
        /// <param name="key">A string specifying the key.</param>
        /// <param name="defaultValue">If <paramref name="key"/> does not exist, return default value.</param>
        /// <param name="refresh">Whether refresh settings before gets value.</param>
        /// <returns>The property object, or <paramref name="defaultValue"/> if <paramref name="key"/> does not exist in the collection.</returns>
        public T GetValue <T>(string section, string key, T defaultValue, bool refresh)
        {
            this.CheckNullValue(section);

            this.CheckNullValue(key);

            if (refresh)
            {
                this.Refresh();
            }

            lock (this._syncRoot)
            {
                if (this._sections.ContainsKey(section))
                {
                    try
                    {
                        T result = XmlConverter.ToObject <T>(this._sections[section][key]);
                        return(result != null ? result : defaultValue);
                    }
                    catch (Exception e)
                    {
                        InternalLogger.Log(e);
                        return(defaultValue);
                    }
                }
                else
                {
                    return(defaultValue);
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Gets value of specified key.
        /// </summary>
        /// <typeparam name="T">Type of value.</typeparam>
        /// <param name="key">A string specifying the key.</param>
        /// <param name="defaultValue">If <paramref name="key"/> does not exist, return default value.</param>
        /// <param name="refresh">Whether refresh settings before gets value.</param>
        /// <returns>A configuration object, or <paramref name="defaultValue"/> if <paramref name="key"/> does not exist in the collection.</returns>
        public T GetValue <T>(string key, T defaultValue, bool refresh)
        {
            this.CheckNullKey(key);

            if (refresh)
            {
                this.Refresh();
            }

            lock (this._syncRoot)
            {
                if (this.ArrayContains(this._configuration.AppSettings.Settings.AllKeys, key))
                {
                    try
                    {
                        return(XmlConverter.ToObject <T>(this._configuration.AppSettings.Settings[key].Value));
                    }
                    catch
                    {
                        return(defaultValue);
                    }
                }

                if (this._settings.Contains(key))
                {
                    return(this._settings.GetValue <T>(key, defaultValue, false));
                }

                return(defaultValue);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Gets value of specified key.
        /// </summary>
        /// <typeparam name="T">Type of value.</typeparam>
        /// <param name="section">A string specifying the section.</param>
        /// <param name="key">A string specifying the key.</param>
        /// <param name="refresh">Whether refresh settings before gets value.</param>
        /// <returns>The property object.</returns>
        public T GetValue <T>(string section, string key, bool refresh = false)
        {
            this.CheckNullValue(section);

            this.CheckNullValue(key);

            if (refresh)
            {
                this.Refresh();
            }

            lock (this._syncRoot)
            {
                if (this._sections.ContainsKey(section))
                {
                    try
                    {
                        return(XmlConverter.ToObject <T>(this._sections[section][key]));
                    }
                    catch (Exception e)
                    {
                        InternalLogger.Log(e);
                        throw;
                    }
                }
                else
                {
                    throw new KeyNotFoundException(string.Format(ConfigurationConstants.KeyNotFoundExceptionStringFormat, section));
                }
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Gets value of specified key.
        /// </summary>
        /// <typeparam name="T">Type of value.</typeparam>
        /// <param name="key">A string specifying the key.</param>
        /// <param name="refresh">Whether refresh settings before gets value.</param>
        /// <returns>A configuration object.</returns>
        public T GetValue <T>(string key, bool refresh = false)
        {
            this.CheckNullKey(key);

            if (refresh)
            {
                this.Refresh();
            }

            this._readerWriterLock.AcquireReaderLock(Timeout.Infinite);

            try
            {
                if (this.ArrayContains(this._configuration.AppSettings.Settings.AllKeys, key))
                {
                    try
                    {
                        return(XmlConverter.ToObject <T>(this._configuration.AppSettings.Settings[key].Value));
                    }
                    catch (Exception e)
                    {
                        InternalLogger.Log(e);
                        throw;
                    }
                }

                if (this._settings.Contains(key))
                {
                    try
                    {
                        return(this._settings.GetValue <T>(key, false));
                    }
                    catch (Exception e)
                    {
                        InternalLogger.Log(e);
                        throw;
                    }
                }

                throw new KeyNotFoundException(string.Format(ConfigurationConstants.KeyNotFoundExceptionStringFormat, key));
            }
            finally
            {
                this._readerWriterLock.ReleaseReaderLock();
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Gets value of specified key.
        /// </summary>
        /// <typeparam name="T">Type of value.</typeparam>
        /// <param name="section">A string specifying the section.</param>
        /// <param name="key">A string specifying the key.</param>
        /// <param name="defaultValue">If <paramref name="key"/> does not exist, return default value.</param>
        /// <param name="refresh">Whether refresh settings before gets value.</param>
        /// <returns>The property object, or <paramref name="defaultValue"/> if <paramref name="key"/> does not exist in the collection.</returns>
        public T GetValue <T>(string section, string key, T defaultValue, bool refresh)
        {
            this.CheckNullValue(section);

            this.CheckNullValue(key);

            if (refresh)
            {
                this.Refresh();
            }

            this._readerWriterLock.AcquireReaderLock(Timeout.Infinite);

            try
            {
                if (this.Sections.ContainsKey(section))
                {
                    try
                    {
                        T result = XmlConverter.ToObject <T>(this.Sections[section][key]);
                        return(result != null ? result : defaultValue);
                    }
                    catch (Exception e)
                    {
                        InternalLogger.Log(e);
                        return(defaultValue);
                    }
                }
                else
                {
                    return(defaultValue);
                }
            }
            catch
            {
                return(defaultValue);
            }
            finally
            {
                this._readerWriterLock.ReleaseReaderLock();
            }
        }
Esempio n. 6
0
        /// <summary>
        /// Gets value of specified key.
        /// </summary>
        /// <typeparam name="T">Type of value.</typeparam>
        /// <param name="key">A string specifying the key.</param>
        /// <param name="defaultValue">If <paramref name="key"/> does not exist, return default value.</param>
        /// <param name="refresh">Whether refresh settings before gets value.</param>
        /// <returns>A configuration object, or <paramref name="defaultValue"/> if <paramref name="key"/> does not exist in the collection.</returns>
        public T GetValue <T>(string key, T defaultValue, bool refresh)
        {
            this.CheckNullKey(key);

            if (refresh)
            {
                this.Refresh();
            }

            this._readerWriterLock.AcquireReaderLock(Timeout.Infinite);

            try
            {
                if (this.ArrayContains(this._configuration.AppSettings.Settings.AllKeys, key))
                {
                    try
                    {
                        return(XmlConverter.ToObject <T>(this._configuration.AppSettings.Settings[key].Value));
                    }
                    catch
                    {
                        return(defaultValue);
                    }
                }

                if (this._settings.Contains(key))
                {
                    return(this._settings.GetValue <T>(key, defaultValue, false));
                }

                return(defaultValue);
            }
            catch
            {
                return(defaultValue);
            }
            finally
            {
                this._readerWriterLock.ReleaseReaderLock();
            }
        }
Esempio n. 7
0
        /// <summary>
        /// Gets value of specified key.
        /// </summary>
        /// <typeparam name="T">Type of value.</typeparam>
        /// <param name="key">A string specifying the key.</param>
        /// <param name="refresh">Whether refresh settings before gets value.</param>
        /// <returns>A configuration object.</returns>
        public T GetValue <T>(string key, bool refresh = false)
        {
            this.CheckNullKey(key);

            if (refresh)
            {
                this.Refresh();
            }

            lock (this._syncRoot)
            {
                if (this.ArrayContains(this._configuration.AppSettings.Settings.AllKeys, key))
                {
                    try
                    {
                        return(XmlConverter.ToObject <T>(this._configuration.AppSettings.Settings[key].Value));
                    }
                    catch (Exception e)
                    {
                        InternalLogger.Log(e);
                        throw;
                    }
                }

                if (this._settings.Contains(key))
                {
                    try
                    {
                        return(this._settings.GetValue <T>(key, false));
                    }
                    catch (Exception e)
                    {
                        InternalLogger.Log(e);
                        throw;
                    }
                }

                throw new KeyNotFoundException(string.Format(ConfigurationConstants.KeyNotFoundExceptionStringFormat, key));
            }
        }
Esempio n. 8
0
        /// <summary>
        /// Gets value of specified key.
        /// </summary>
        /// <typeparam name="T">Type of value.</typeparam>
        /// <param name="section">A string specifying the section.</param>
        /// <param name="key">A string specifying the key.</param>
        /// <param name="refresh">Whether refresh settings before gets value.</param>
        /// <returns>The property object.</returns>
        public T GetValue <T>(string section, string key, bool refresh = false)
        {
            this.CheckNullValue(section);

            this.CheckNullValue(key);

            if (refresh)
            {
                this.Refresh();
            }

            this._readerWriterLock.AcquireReaderLock(Timeout.Infinite);

            try
            {
                if (this.Sections.ContainsKey(section))
                {
                    try
                    {
                        return(XmlConverter.ToObject <T>(this.Sections[section][key]));
                    }
                    catch (Exception e)
                    {
                        InternalLogger.Log(e);
                        throw;
                    }
                }
                else
                {
                    throw new KeyNotFoundException(string.Format(ConfigurationConstants.KeyNotFoundExceptionStringFormat, section));
                }
            }
            finally
            {
                this._readerWriterLock.ReleaseReaderLock();
            }
        }