示例#1
0
文件: Config.cs 项目: cang2016/RMS
        /// <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.m_configuration.AppSettings.Settings.AllKeys, key))
                {
                    try
                    {
                        return(XmlConverter.ToObject <T>(this.m_configuration.AppSettings.Settings[key].Value));
                    }
                    catch (Exception e)
                    {
                        InternalLogger.Log(e);
                        throw;
                    }
                }

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

                throw new KeyNotFoundException(string.Format(ConfigurationConstants.KeyNotFoundExceptionStringFormat, key));
            }
            finally
            {
                this._readerWriterLock.ReleaseReaderLock();
            }
        }
示例#2
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.m_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.m_readerWriterLock.ReleaseReaderLock();
            }
        }
示例#3
0
文件: Config.cs 项目: cang2016/RMS
        /// <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.m_configuration.AppSettings.Settings.AllKeys, key))
                {
                    try
                    {
                        return(XmlConverter.ToObject <T>(this.m_configuration.AppSettings.Settings[key].Value));
                    }
                    catch
                    {
                        return(defaultValue);
                    }
                }

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

                return(defaultValue);
            }
            catch
            {
                return(defaultValue);
            }
            finally
            {
                this._readerWriterLock.ReleaseReaderLock();
            }
        }
示例#4
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.m_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.m_readerWriterLock.ReleaseReaderLock();
            }
        }