예제 #1
0
        /// <summary>
        /// Gets a settings value by its settings path in a specific type format.
        /// </summary>
        /// <typeparam name="T">The type of the requested settings value.</typeparam>
        /// <param name="path">The path to get the setting from.</param>
        /// <returns>The casted object instance of of the setting of type <typeparamref name="T"/> defined in the settings mapping.</returns>
        public virtual T GetValue <T>(string path)
        {
            T value;

            if (!TryGetValue <T>(path, out value) && FallbackMap != null)
            {
                FallbackMap.TryGetValue <T>(path, out value);
            }
            return(value);
        }
예제 #2
0
        /// <summary>
        /// Gets an array of elements of a specific type defined by the settings path.
        /// </summary>
        /// <typeparam name="T">The type of the elements.</typeparam>
        /// <param name="path">The path to the requested settings array.</param>
        /// <returns>An enumerable collection of elements of type <typeparamref name="T"/>.</returns>
        public virtual IEnumerable <T> GetArray <T>(string path)
        {
            var converter = GetTypeConverter(typeof(T));

            object rawValue;

            if (!_nodes.TryGetValue(path, out rawValue))
            {
                if (FallbackMap != null)
                {
                    foreach (var element in FallbackMap.GetArray <T>(path))
                    {
                        yield return(element);
                    }
                }
            }
            else
            {
                var rawArray = rawValue as string[];

                if (rawArray != null)
                {
                    for (int i = 0; i < rawArray.Length; i++)
                    {
                        bool yieldElement = true;
                        T    element      = default(T);

                        try
                        {
                            element      = (T)converter.ConvertFrom(rawArray[i]);
                            yieldElement = true;
                        }
                        catch
                        {
                            yieldElement = false;
                        }

                        if (yieldElement)
                        {
                            yield return(element);
                        }
                    }
                }
            }
        }