Beispiel #1
0
        private static T GetConfigValue <T>(string configName, T?minValue, T?maxValue, T defaultValue, AppConfigLoader.TryParseValue <T> tryParseValue) where T : struct, IComparable <T>
        {
            string text = null;

            if (!AppConfigLoader.TryGetConfigRawValue(configName, out text))
            {
                return(defaultValue);
            }
            T t = defaultValue;

            if (!tryParseValue(text, out t))
            {
                AppConfigLoader.Tracer.TraceWarning <string, string>(0L, "cannot apply config {0} with invalid value: {1}", configName, text);
                return(defaultValue);
            }
            if (minValue != null && t.CompareTo(minValue.Value) < 0)
            {
                AppConfigLoader.Tracer.TraceWarning <string, T, T>(0L, "cannot apply config:{0}, value:{1} is less than minValue:{2}", configName, t, minValue.Value);
                return(defaultValue);
            }
            if (maxValue != null && t.CompareTo(maxValue.Value) > 0)
            {
                AppConfigLoader.Tracer.TraceWarning <string, T, T>(0L, "cannot apply config:{0}, value:{1} is greater than maxValue:{2}", configName, t, maxValue.Value);
                return(defaultValue);
            }
            return(t);
        }
Beispiel #2
0
        public static string GetConfigStringValue(string configName, string defaultValue)
        {
            string result = null;

            if (!AppConfigLoader.TryGetConfigRawValue(configName, out result))
            {
                return(defaultValue);
            }
            return(result);
        }
Beispiel #3
0
        public static T GetConfigEnumValue <T>(string configName, T defaultValue)
        {
            string text = null;

            if (!AppConfigLoader.TryGetConfigRawValue(configName, out text))
            {
                return(defaultValue);
            }
            T result;

            try
            {
                T t = (T)((object)Enum.Parse(typeof(T), text, true));
                result = t;
            }
            catch (ArgumentException arg)
            {
                AppConfigLoader.Tracer.TraceWarning <string, string, ArgumentException>(0L, "Invalid Value:{0}, for config:{1}, parsing failed with error:{2}", text, configName, arg);
                result = defaultValue;
            }
            return(result);
        }