Exemplo n.º 1
0
        /// <summary>
        /// 根据配置名获取配置
        /// </summary>
        /// <typeparam name="T">配置最终转换到的类型</typeparam>
        /// <param name="name">配置所属类型的名字</param>
        /// <param name="def">当找不到配置时的默认值</param>
        /// <returns>配置的值,如果找不到则返回默认值</returns>
        public T Get <T>(string name, T def = default(T))
        {
            Guard.Requires <AssertException>(locator != null);
            Guard.Requires <AssertException>(converters != null);
            Guard.Requires <ArgumentNullException>(name != null);

            string val;

            if (!locator.TryGetValue(name, out val))
            {
                return(def);
            }

            object result;

            return(converters.TryConvert(val, out result, typeof(T)) ? (T)result : def);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 根据配置名获取配置
        /// </summary>
        /// <param name="name">配置所属类型的名字</param>
        /// <param name="type">配置最终转换到的类型</param>
        /// <param name="def">当找不到配置时的默认值</param>
        /// <returns>配置的值,如果找不到则返回默认值</returns>
        public object Get(string name, Type type, object def)
        {
            Guard.Requires <AssertException>(locator != null);
            Guard.Requires <AssertException>(converters != null);
            Guard.Requires <ArgumentNullException>(name != null);

            string val;

            if (!locator.TryGetValue(name, out val))
            {
                return(def);
            }

            object result;

            return(converters.TryConvert(val, out result, type) ? result : def);
        }