Beispiel #1
0
        /// <summary>
        /// Parse the value for a field from its text based representation in an XML file
        /// </summary>
        /// <param name="FieldType">The type of field being read</param>
        /// <param name="Text">Text to parse</param>
        /// <returns>The object that was parsed</returns>
        static object ParseValue(Type FieldType, string Text)
        {
            // ignore whitespace in all fields except for Strings which we leave unprocessed
            string TrimmedText = Text.Trim();

            if (FieldType == typeof(string))
            {
                return(Text);
            }
            else if (FieldType == typeof(bool) || FieldType == typeof(bool?))
            {
                if (TrimmedText == "1" || TrimmedText.Equals("true", StringComparison.InvariantCultureIgnoreCase))
                {
                    return(true);
                }
                else if (TrimmedText == "0" || TrimmedText.Equals("false", StringComparison.InvariantCultureIgnoreCase))
                {
                    return(false);
                }
                else
                {
                    throw new Exception(String.Format("Unable to convert '{0}' to boolean. 'true/false/0/1' are the supported formats.", Text));
                }
            }
            else if (FieldType == typeof(int))
            {
                return(Int32.Parse(TrimmedText));
            }
            else if (FieldType == typeof(float))
            {
                return(Single.Parse(TrimmedText, System.Globalization.CultureInfo.InvariantCulture));
            }
            else if (FieldType == typeof(double))
            {
                return(Double.Parse(TrimmedText, System.Globalization.CultureInfo.InvariantCulture));
            }
            else if (FieldType.IsEnum)
            {
                return(Enum.Parse(FieldType, TrimmedText));
            }
            else if (FieldType == typeof(FileReference))
            {
                return(FileReference.FromString(Text));
            }
            else
            {
                throw new Exception(String.Format("Unsupported config type '{0}'", FieldType.Name));
            }
        }