コード例 #1
0
        private void AssertLocalDateTimeConvertersAreRegistered()
        {
            var localDateTimeToDateTimeConverter = AutoMappingUtils.GetConverter(typeof(LocalDateTime), typeof(DateTime));

            Assert.IsNotNull(localDateTimeToDateTimeConverter);

            var dateTimeToLocalDateTimeConverter = AutoMappingUtils.GetConverter(typeof(DateTime), typeof(LocalDateTime));

            Assert.IsNotNull(dateTimeToLocalDateTimeConverter);
        }
コード例 #2
0
        private void AssertZonedDateTimeConvertersAreRegistered()
        {
            var zonedDateTimeToDateTimeOffsetConverter = AutoMappingUtils.GetConverter(typeof(ZonedDateTime), typeof(DateTimeOffset));

            Assert.IsNotNull(zonedDateTimeToDateTimeOffsetConverter);

            var dateTimeOffsetToZonedDateTimeOffsetConverter = AutoMappingUtils.GetConverter(typeof(DateTimeOffset), typeof(ZonedDateTime));

            Assert.IsNotNull(dateTimeOffsetToZonedDateTimeOffsetConverter);
        }
コード例 #3
0
        private void AssertLocalTimeConvertersAreRegistered()
        {
            var localTimeToTimeSpanConverter = AutoMappingUtils.GetConverter(typeof(LocalTime), typeof(TimeSpan));

            Assert.IsNotNull(localTimeToTimeSpanConverter);

            var timeSpanToLocalTimeConverter = AutoMappingUtils.GetConverter(typeof(TimeSpan), typeof(LocalTime));

            Assert.IsNotNull(timeSpanToLocalTimeConverter);
        }
コード例 #4
0
        private void AssertLocalTimeConvertersAreRegistered()
        {
            var localTimeToTimeSpanConverter = AutoMappingUtils.GetConverter(typeof(LocalTime), typeof(TimeSpan));

            localTimeToTimeSpanConverter.Should().NotBeNull();

            var localTimeToNullableTimeSpanConverter = AutoMappingUtils.GetConverter(typeof(LocalTime), typeof(TimeSpan?));

            localTimeToNullableTimeSpanConverter.Should().NotBeNull();

            var timeSpanToLocalTimeConverter = AutoMappingUtils.GetConverter(typeof(TimeSpan), typeof(LocalTime));

            timeSpanToLocalTimeConverter.Should().NotBeNull();

            var nullableTimeSpanToLocalTimeConverter = AutoMappingUtils.GetConverter(typeof(TimeSpan?), typeof(LocalTime));

            nullableTimeSpanToLocalTimeConverter.Should().NotBeNull();
        }
コード例 #5
0
        private void AssertLocalDateConvertersAreRegistered()
        {
            var localDateToDateTimeConverter = AutoMappingUtils.GetConverter(typeof(LocalDate), typeof(DateTime));

            localDateToDateTimeConverter.Should().NotBeNull();

            var localDateToNullableDateTimeConverter = AutoMappingUtils.GetConverter(typeof(LocalDate), typeof(DateTime?));

            localDateToNullableDateTimeConverter.Should().NotBeNull();

            var dateTimeToLocalDateTimeConverter = AutoMappingUtils.GetConverter(typeof(DateTime), typeof(LocalDate));

            dateTimeToLocalDateTimeConverter.Should().NotBeNull();

            var nullableDateTimeToLocalDateTimeConverter = AutoMappingUtils.GetConverter(typeof(DateTime?), typeof(LocalDate));

            nullableDateTimeToLocalDateTimeConverter.Should().NotBeNull();
        }
コード例 #6
0
        private void AssertZonedDateTimeConvertersAreRegistered()
        {
            var zonedDateTimeToDateTimeOffsetConverter = AutoMappingUtils.GetConverter(typeof(ZonedDateTime), typeof(DateTimeOffset));

            zonedDateTimeToDateTimeOffsetConverter.Should().NotBeNull();

            var zonedDateTimeToNullableDateTimeOffsetConverter = AutoMappingUtils.GetConverter(typeof(ZonedDateTime), typeof(DateTimeOffset?));

            zonedDateTimeToNullableDateTimeOffsetConverter.Should().NotBeNull();

            var dateTimeOffsetToZonedDateTimeConverter = AutoMappingUtils.GetConverter(typeof(DateTimeOffset), typeof(ZonedDateTime));

            dateTimeOffsetToZonedDateTimeConverter.Should().NotBeNull();

            var nullableDateTimeOffsetToZonedDateTimeConverter = AutoMappingUtils.GetConverter(typeof(DateTimeOffset?), typeof(ZonedDateTime));

            nullableDateTimeOffsetToZonedDateTimeConverter.Should().NotBeNull();
        }
コード例 #7
0
        public static GetMemberDelegate CreateTypeConverter(Type fromType, Type toType)
        {
            if (fromType == toType)
            {
                return(null);
            }

            var converter = AutoMappingUtils.GetConverter(fromType, toType);

            if (converter != null)
            {
                return(converter);
            }

            if (fromType == typeof(string))
            {
                return(fromValue => TypeSerializer.DeserializeFromString((string)fromValue, toType));
            }

            if (toType == typeof(string))
            {
                return(o => TypeSerializer.SerializeToString(o).StripQuotes());
            }

            var underlyingToType   = Nullable.GetUnderlyingType(toType) ?? toType;
            var underlyingFromType = Nullable.GetUnderlyingType(fromType) ?? fromType;

            if (underlyingToType.IsEnum)
            {
                if (underlyingFromType.IsEnum || fromType == typeof(string))
                {
                    return(fromValue => Enum.Parse(underlyingToType, fromValue.ToString(), ignoreCase: true));
                }

                if (underlyingFromType.IsIntegerType())
                {
                    return(fromValue => Enum.ToObject(underlyingToType, fromValue));
                }
            }
            else if (underlyingFromType.IsEnum)
            {
                if (underlyingToType.IsIntegerType())
                {
                    return(fromValue => Convert.ChangeType(fromValue, underlyingToType, null));
                }
            }
            else if (typeof(IEnumerable).IsAssignableFrom(fromType) && underlyingToType != typeof(string))
            {
                return(fromValue => AutoMappingUtils.TryConvertCollections(fromType, underlyingToType, fromValue));
            }
            else if (underlyingToType.IsValueType)
            {
                return(fromValue => AutoMappingUtils.ChangeValueType(fromValue, underlyingToType));
            }
            else
            {
                return(fromValue =>
                {
                    if (fromValue == null)
                    {
                        return fromValue;
                    }
                    if (toType == typeof(string))
                    {
                        return fromValue.ToJsv();
                    }

                    var toValue = toType.CreateInstance();
                    toValue.PopulateWith(fromValue);
                    return toValue;
                });
            }

            return(null);
        }