예제 #1
0
        public static T GetValue <T>(string keyName)
        {
            if (string.IsNullOrEmpty(keyName))
            {
                ThrowException.ThrowArgumentNullException("keyName");
            }

            string value = ConfigurationManager.AppSettings[keyName];

            if (value == null)
            {
                ThrowException.ThrowConfigurationErrorsException(string.Format("Cannot find the '{0}' configuration key from the AppSettings section", keyName));
            }

            return(TypeHelper.To <T>(value));
        }
예제 #2
0
        public static T GetValue <T>(string keyName, T defaultValue = default(T))
        {
            if (string.IsNullOrEmpty(keyName))
            {
                ThrowException.ThrowArgumentNullException("keyName");
            }

            T value = defaultValue;

            string sValue = ConfigurationManager.AppSettings[keyName];

            if (sValue != null)
            {
                value = TypeHelper.To <T>(sValue);
            }

            return(value);
        }
예제 #3
0
        /// <summary>
        /// Gets an object from the cache given its unique ID.
        /// </summary>
        ///
        /// <typeparam name="T">
        /// The type of the retrieved object.
        /// </typeparam>
        ///
        /// <param name="key">
        /// Unique key.
        /// </param>
        ///
        /// <returns>
        /// The retrieved object. If not found returns null.
        /// </returns>
        public T Get <T>(string key) where T : class
        {
            T item = default(T);

            var statistics = _statistics as CacheStatistics;

            statistics.IncrementRequestCount();

            if (_items.ContainsKey(key))
            {
                lock (_locker)
                {
                    if (_items.ContainsKey(key))
                    {
                        if (_items[key].IsExpired)
                        {
                            this.Remove(key);
                        }
                        else
                        {
                            item = TypeHelper.To <T>(_items[key].Data);
                        }
                    }
                }
            }

            if (item == null)
            {
                statistics.IncrementMissCount();
            }
            else
            {
                statistics.IncrementHitCount();
            }

            return(item);
        }
예제 #4
0
        public static IEnumerable <T> GetValues <T>(string keyName, params char[] separators)
        {
            if (string.IsNullOrEmpty(keyName))
            {
                ThrowException.ThrowArgumentNullException("keyName");
            }

            if (separators.IsNullOrEmpty())
            {
                ThrowException.ThrowArgumentNullException("separators");
            }

            string values = ConfigurationManagerHelper.GetValue <string>(keyName);

            return(new List <T>(values.Split(separators, StringSplitOptions.RemoveEmptyEntries).Select(i => TypeHelper.To <T>(i))));
        }
예제 #5
0
 /// <summary>
 /// Converts a DataReader item to given type.
 /// </summary>
 ///
 /// <typeparam name="T">
 /// Convertion type.
 /// </typeparam>
 ///
 /// <param name="dbReader">
 /// DataReader object.
 /// </param>
 ///
 /// <param name="columnName">
 /// Column name.
 /// </param>
 ///
 /// <returns>
 /// Converted object.
 /// </returns>
 public static T To <T>(IDataReader dbReader, string columnName)
 {
     return(TypeHelper.To <T>(dbReader[columnName]));
 }
예제 #6
0
 /// <summary>
 /// Converts a DataRow item to given type.
 /// </summary>
 ///
 /// <typeparam name="T">
 /// Convertion type.
 /// </typeparam>
 ///
 /// <param name="row">
 /// DataRow object.
 /// </param>
 ///
 /// <param name="columnName">
 /// Column name.
 /// </param>
 ///
 /// <returns>
 /// Converted object.
 /// </returns>
 public static T To <T>(DataRow row, string columnName)
 {
     return(TypeHelper.To <T>(row[columnName]));
 }
예제 #7
0
 /// <summary>
 /// Retrieves the value associated with the specified name, in the specified registry key.
 /// If the name is not found in the specified key, returns a default value that you provide, or null if the specified key does not exist.
 /// </summary>
 ///
 /// <typeparam name="T">
 /// Type of the returned value.
 /// </typeparam>
 ///
 /// <param name="keyName">
 /// The full registry path of the key, beginning with a valid registry root, such as "HKEY_CURRENT_USER".
 /// </param>
 ///
 /// <param name="valueName">
 /// The name of the name/value pair.
 /// </param>
 ///
 /// <param name="defaultValue">
 /// The value to return if valueName does not exist.
 /// </param>
 ///
 /// <returns>
 /// null if the subkey specified by keyName does not exist; otherwise, the value associated with valueName, or defaultValue if valueName is not found.
 /// </returns>
 public static T GetValue <T>(string keyName, string valueName, T defaultValue = default(T))
 {
     return(TypeHelper.To <T>(Registry.GetValue(keyName, valueName, defaultValue)));
 }