Ejemplo n.º 1
0
        /// <summary>
        /// Gets a value from the ini file.
        /// </summary>
        /// <typeparam name="T">The type of value to return.</typeparam>
        /// <param name="section">The section the value belongs to.</param>
        /// <param name="key">The key to the value.</param>
        /// <param name="defaultValue">The default value to return if no value was found. Optional.</param>
        /// <returns>The value, or defaultValue if the value doesn't exist.</returns>
        public T GetValue <T>(string section, string key, T defaultValue = default(T))
        {
            if (!ValueExists(section, key))
            {
                if ((defaultValue == null) && (typeof(T) == typeof(string)))
                {
                    return((T)Convert.ChangeType("", typeof(T)));
                }
                return(defaultValue);
            }

            object val = ReadValue(section, key) ?? "";

            try
            {
                if (typeof(T) == typeof(string))
                {
                    val = val.ToString();
                }
                else if (typeof(T) == typeof(bool))
                {
                    val = HQTools.StringToBool((string)val);
                }
                else if (typeof(T) == typeof(int))
                {
                    val = HQTools.StringToInt((string)val);
                }
                else if (typeof(T) == typeof(float))
                {
                    val = HQTools.StringToFloat((string)val);
                }
                else if (typeof(T) == typeof(double))
                {
                    val = HQTools.StringToDouble((string)val);
                }
                else if (typeof(T) == typeof(Coordinates))
                {
                    val = new Coordinates(val.ToString());
                }
                else if (typeof(T) == typeof(MinMaxI))
                {
                    val = new MinMaxI(val.ToString());
                }
                else if (typeof(T) == typeof(MinMaxD))
                {
                    val = new MinMaxD(val.ToString());
                }
                else if (typeof(T).IsEnum)
                {
                    val = Enum.Parse(typeof(T), val.ToString(), true);
                }

                return((T)Convert.ChangeType(val, typeof(T)));
            }
            catch (Exception)
            {
                return(default(T));
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Converts a string array to an array of type T.
        /// </summary>
        /// <typeparam name="T">The type of the source array.</typeparam>
        /// <param name="sourceArray">The source string array.</param>
        /// <returns>The converted array</returns>
        private T[] ConvertArray <T>(string[] sourceArray)
        {
            try
            {
                T[] arr = new T[sourceArray.Length];

                for (int i = 0; i < sourceArray.Length; i++)
                {
                    object o = default(T);

                    if (typeof(T) == typeof(bool))
                    {
                        o = HQTools.StringToBool(sourceArray[i]);
                    }
                    else if (typeof(T) == typeof(int))
                    {
                        o = HQTools.StringToInt(sourceArray[i]);
                    }
                    else if (typeof(T) == typeof(double))
                    {
                        o = HQTools.StringToDouble(sourceArray[i]);
                    }
                    else if (typeof(T) == typeof(float))
                    {
                        o = HQTools.StringToFloat(sourceArray[i]);
                    }
                    else if (typeof(T).IsEnum)
                    {
                        o = Enum.Parse(typeof(T), sourceArray[i].ToString(), true);
                    }

                    arr[i] = (T)Convert.ChangeType(o, typeof(T));
                }

                return(arr);
            }
            catch (Exception)
            {
                return(new T[0]);
            }
        }