Exemplo n.º 1
0
        static bool TryParseHexColor(string text, out FastColour result)
        {
            result = FastColour.White;
            byte red, green, blue;

            if (!(text.Length == 6 || text.Length == 7))
            {
                return(false);
            }

            int offset = text[0] == '#' ? 1 : 0;

            if (!TryParseByteHex(text, offset, out red) ||
                !TryParseByteHex(text, offset + 2, out green) ||
                !TryParseByteHex(text, offset + 4, out blue))
            {
                return(false);
            }

            result = new FastColour(red, green, blue, 255);
            return(true);
        }
Exemplo n.º 2
0
        /// <summary> Attempts to parse the value of the given configuration key as the specified type. </summary>
        /// <param name="configKey"> The key of the configuration option. </param>
        /// <param name="defaultValue"> The default value to assign if parsing fails. </param>
        /// <param name="value"> The configuration value assigned by the method. </param>
        /// <returns> true on success, false on failure. The method can fail if the key is null,
        /// the key is not found, or if the value could not be parsed. </returns>
        /// <remarks> T can be one of the following types: bool, char, sbyte, short, int, long, byte,
        /// ushort, uint, ulong, float, double, decimal, DateTime, Enum. </remarks>
        public bool TryParseValueOrDefault <T>(string configKey, T defaultValue, out T value) where T : struct, IConvertible
        {
            if (configKey == null)
            {
                value = defaultValue;
                return(false);
            }
            else
            {
                Type   type          = typeof(T);
                bool   parsingWorked = false;
                string rawValue;
                if (!config.TryGetValue(configKey, out rawValue))
                {
                    value = defaultValue;
                    Console.WriteLine("Couldn't find key {0} in config. Setting to default value of {1}.", configKey, defaultValue);
                    return(false);
                }

                // Switch case doesn't work, and we can't do straight casts in an out parameter.
                // Ugly if else chain it is.
                if (type == typeof(bool))
                {
                    bool result;
                    parsingWorked = Boolean.TryParse(rawValue, out result);
                    value         = (T)(ValueType)result;
                }
                else if (type == typeof(char))
                {
                    char result;
                    parsingWorked = Char.TryParse(rawValue, out result);
                    value         = (T)(ValueType)result;
                }
                else if (type == typeof(sbyte))
                {
                    sbyte result;
                    parsingWorked = SByte.TryParse(rawValue, out result);
                    value         = (T)(ValueType)result;
                }
                else if (type == typeof(short))
                {
                    short result;
                    parsingWorked = Int16.TryParse(rawValue, out result);
                    value         = (T)(ValueType)result;
                }
                else if (type == typeof(int))
                {
                    int result;
                    parsingWorked = Int32.TryParse(rawValue, out result);
                    value         = (T)(ValueType)result;
                }
                else if (type == typeof(long))
                {
                    long result;
                    parsingWorked = Int64.TryParse(rawValue, out result);
                    value         = (T)(ValueType)result;
                }
                else if (type == typeof(byte))
                {
                    byte result;
                    parsingWorked = Byte.TryParse(rawValue, out result);
                    value         = (T)(ValueType)result;
                }
                else if (type == typeof(ushort))
                {
                    ushort result;
                    parsingWorked = UInt16.TryParse(rawValue, out result);
                    value         = (T)(ValueType)result;
                }
                else if (type == typeof(uint))
                {
                    uint result;
                    parsingWorked = UInt32.TryParse(rawValue, out result);
                    value         = (T)(ValueType)result;
                }
                else if (type == typeof(ulong))
                {
                    ulong result;
                    parsingWorked = UInt64.TryParse(rawValue, out result);
                    value         = (T)(ValueType)result;
                }
                else if (type == typeof(float))
                {
                    float result;
                    parsingWorked = Single.TryParse(rawValue, out result);
                    value         = (T)(ValueType)result;
                }
                else if (type == typeof(double))
                {
                    double result;
                    parsingWorked = Double.TryParse(rawValue, out result);
                    value         = (T)(ValueType)result;
                }
                else if (type == typeof(decimal))
                {
                    decimal result;
                    parsingWorked = Decimal.TryParse(rawValue, out result);
                    value         = (T)(ValueType)result;
                }
                else if (type == typeof(DateTime))
                {
                    DateTime result;
                    parsingWorked = DateTime.TryParse(rawValue, out result);
                    value         = (T)(ValueType)result;
                }
                else if (type == typeof(FastColour))
                {
                    FastColour result;
                    parsingWorked = FastColour.TryParse(rawValue, out result);
                    value         = (T)(ValueType)result;
                }
                else if (type.IsEnum)
                {
                    try {
                        value         = (T)(ValueType)Enum.Parse(type, rawValue, true);                   // Case insensitive.
                        parsingWorked = true;
                    } catch (ArgumentException) {
                        value         = defaultValue;                 // Stops compilation errors.
                        parsingWorked = false;
                    }
                }
                else
                {
                    value = defaultValue;
                    throw new InvalidCastException("Cannot parse type " + type.Name);
                }
                if (!parsingWorked)
                {
                    Console.WriteLine("Couldn't parse value for {0} from config. Setting to default value of {1}.", configKey, defaultValue);
                    value = defaultValue;
                }
                return(parsingWorked);
            }
        }