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
 public static Coordinates CreateRandomInaccuracy(MinMaxD minMax)
 {
     return(FromAngleInDegrees(HQTools.RandomDouble(360.0)) * minMax.GetValue());
 }