Пример #1
0
        /// <summary>
        /// Convert from a string to the required type of value.
        /// This is used when setting field values from strings (eg user input, http request or importing a file).
        /// Used in the core by ActiveField and Sqlize
        /// It is exposed as a public utility method so it can be used other places too.
        /// </summary>
        public static object FromString(string stringValue, Type correctType)
        {
            if (stringValue.IsBlank())
            {
                // set to default which is null for nullable types or false for bool
                return(GetDefaultValue(correctType));
            }
            stringValue = stringValue.Trim();

            // prep value for conversion
            TypeConverter conv      = TypeDescriptor.GetConverter(correctType);
            object        thisValue = stringValue;

            // do any prep work depending on type
            if (correctType.FullName.Contains("System.Boolean"))                //this is for bool or nullable bool
            {
                if (thisValue + "" == "1")
                {
                    thisValue = "True";
                }
                if (thisValue + "" == "0")
                {
                    thisValue = "False";
                }
                string lowerVal = (thisValue + "").ToLower();
                if (lowerVal == "yes")
                {
                    thisValue = "True";
                }
                if (lowerVal == "no")
                {
                    thisValue = "False";
                }
                if (lowerVal == "on")
                {
                    thisValue = "True";
                }
                if (lowerVal == "off")
                {
                    thisValue = "False";
                }
                if (lowerVal == "t")
                {
                    thisValue = "True";
                }
                if (lowerVal == "f")
                {
                    thisValue = "False";
                }
            }
            else if (correctType.FullName.Contains("System.DateTime"))
            {
                // todo - there must be some prep here for unusual date formats
            }
            else if (correctType.FullName.Contains("System.Decimal"))
            {
                thisValue = Fmt.CleanNumber(stringValue);
            }
            else if (correctType.FullName.Contains("System.Int"))
            {
                if (stringValue == "on")                     // "on" allows you to select null in a radio button group with int values (eg IDs)
                {
                    return(GetDefaultValue(correctType));
                }
                thisValue = "" + Fmt.CleanInt(stringValue);
            }

            // now convert it
            if (conv.CanConvertFrom(typeof(string)))
            {
                try {
                    return(conv.ConvertFrom(thisValue));
                } catch (Exception e) {
                    // MN changed from: catch (FormatException e) {
                    string message = "FromString: '" + stringValue + "' is not a valid value of type " + correctType.Name + "; " + e.Message;

                    if (!e.Message.Contains("Guid should contain 32 digit"))
                    {
                        if (thisValue + "" != "" && !ActiveFieldBase.IgnoreConversionErrors)
                        {
                            throw new ActiveRecordException(message, e);
                        }
                    }
                }
            }
            else
            {
                // TODO: Why do we throw an exception here instead of setting "ex"?
                throw new FormatException("No type converter available for type: " + correctType.Name);
            }
            return(GetDefaultValue(correctType));
        }