示例#1
0
        private T GetConfig <T>(T defaultVal, params string[] path)
        {
            var data = Config.Get(path);

            if (data != null)
            {
                return(Config.ConvertValue <T>(data));
            }
            Config.Set(path.Concat(new object[] { defaultVal }).ToArray());
            ConfigChanged = true;
            return(defaultVal);
        }
示例#2
0
        private bool GetConfigValue <T>(out T value, params string[] path)
        {
            var configValue = Config.Get(path);

            if (configValue == null)
            {
                value = default(T);
                return(false);
            }
            value = Config.ConvertValue <T>(configValue);
            return(true);
        }
示例#3
0
        T GetConfig <T>(T defaultValue, string firstKey, string secondKey = null, string thirdKey = null)
        {
            try
            {
                object value;

                // get the value associated with the provided keys
                if (thirdKey != null)
                {
                    value = Config[firstKey, secondKey, thirdKey];
                }
                else if (secondKey != null)
                {
                    value = Config[firstKey, secondKey];
                }
                else
                {
                    value = Config[firstKey];
                }

                // if the value is a dictionary, add the key/value pairs to a dictionary and return it
                // this particular implementation only handles dictionarys with string key/value pairs
                if (defaultValue.GetType() == typeof(Dictionary <string, int>))           // checks if the value is a dictionary
                {
                    Dictionary <string, int> valueDictionary = Config.ConvertValue <Dictionary <string, int> >(value);

                    return((T)Convert.ChangeType(valueDictionary, typeof(T)));
                }
                if (defaultValue.GetType() == typeof(Dictionary <int, int>))           // checks if the value is a dictionary
                {
                    Dictionary <string, int> valueDictionary = Config.ConvertValue <Dictionary <string, int> >(value);
                    Dictionary <int, int>    values          = valueDictionary.Keys.ToDictionary(int.Parse, key => (int)valueDictionary[key]);

                    return((T)Convert.ChangeType(values, typeof(T)));
                }
                // if the value is a list, add the list elements to a list and return it
                // this particular implementation only handles lists with char elements
                else if (value.GetType().IsGenericType&& value.GetType().GetGenericTypeDefinition() == typeof(List <>))             // checks if the value is a list
                {
                    IList       valueList = (IList)value;
                    List <char> values    = new List <char>();

                    foreach (object obj in valueList)
                    {
                        if (obj is string)
                        {
                            char result;
                            if (char.TryParse((string)obj, out result))
                            {
                                values.Add(result);
                            }
                        }
                    }
                    return((T)Convert.ChangeType(values, typeof(T)));
                }
                // handles every other type
                else
                {
                    return((T)Convert.ChangeType(value, typeof(T)));
                }
            }
            catch (Exception)
            {
                return(defaultValue);
            }
        }