public static object Convert(object value, Type type)
        {
            if (value == null)
            {
                if (IsNullable(type) == false)
                {
                    throw new Exception("Given type " + type.Name + " cannot be null.");
                }
                else
                {
                    return(null);
                }
            }


            if (type.IsEnumeration() == true)
            {
                return(Enum.Parse(type, value.ToString(), true));
            }

            // Handle string to date time conversion
            if (type == typeof(DateTime))
            {
                DateTime result;
                // Return parsed date in local time zone
                if (DateTime.TryParseExact(value.ToString(), new[] { Formats.DateTime, Formats.Date }, null, System.Globalization.DateTimeStyles.AdjustToUniversal, out result) == true)
                {
                    return(result.ToLocalTime());
                }
                else
                {
                    throw new Exception("Unsupported date time format.");
                }
            }
            // Handle Datetime to string conversion.
            if (type == typeof(string) && value is DateTime)
            {
                return(((DateTime)value).ToString("o"));
            }
            // Handle geocode to string conversion
            if (type == typeof(Geocode) && value is string)
            {
                Geocode geo = null;
                if (Geocode.TryParse(value as string, out geo) == false)
                {
                    throw new Exception("Cannot convert " + value + " to a valid geocode.");
                }
                return(geo);
            }

            if (type.IsPrimitiveType() == true || type == typeof(string))
            {
                return(System.Convert.ChangeType(value, type, null));
            }
            throw new Exception("No automatic translation available for type " + type.Name + ".");
        }
示例#2
0
        public void InvalidGeocodeTest()
        {
            var invalid = new[]
            {
                null,               // null
                string.Empty,       // empty
                " ",                // space
                "10,10,123",        // incorrect number of args
                "abc,abc",          // non numeric values
                "91,100",           // incorrect latitude
                "75,190",           // incorrect longitude
            };

            Array.ForEach(invalid, value =>
            {
                Geocode geocode = null;
                Assert.IsFalse(Geocode.TryParse(value, out geocode), "Invalid value {0} parsed successfully as {1}", value, geocode);
                Assert.IsNull(geocode, "Invalid value {0} parsed successfully as {1}", value, geocode);
            });
        }
示例#3
0
        public void GeocodeParsingTest()
        {
            var geo   = new Geocode(10.0m, 10.0m);
            var valid = new[]
            {
                "10,10",        // without decimal points
                "10.0,10.0",    // normal
                " 10.0,10.0",   // with leading spaces
                "10.0,10.0",    // with trailing spaces
                "10.0 ,10.0 ",  // with leading space before comma
                "10.0, 10.0"    // with trailing space after comma
            };

            Array.ForEach(valid, value =>
            {
                Geocode geocode = null;
                Assert.IsTrue(Geocode.TryParse(value, out geocode), "Valid value {0} was not parsed correctly.", value);
                Assert.IsNotNull(geocode, "Geocode parsed for {0} was null.", value);
                Assert.IsTrue(geocode.Equals(geo), "Expected {0} but received {1}.", geo.ToString(), geocode.ToString());
            });
        }