コード例 #1
0
        private static Func <object, object> GetConverter(StandardMapper mapper, PocoColumn pc, Type srcType, Type dstType)
        {
            Func <object, object> converter = null;

            // Get converter from the mapper
            if (pc != null)
            {
                converter = mapper.GetFromDbConverter(pc.PropertyInfo, srcType);
                if (converter != null)
                {
                    return(converter);
                }
            }

            // Standard DateTime->Utc mapper
            if (pc != null && pc.ForceToUtc && srcType == typeof(DateTime) && (dstType == typeof(DateTime) || dstType == typeof(DateTime?)))
            {
                return(delegate(object src) { return new DateTime(((DateTime)src).Ticks, DateTimeKind.Utc); });
            }

            // unwrap nullable types
            Type underlyingDstType = Nullable.GetUnderlyingType(dstType);

            if (underlyingDstType != null)
            {
                dstType = underlyingDstType;
            }

            // Forced type conversion including integral types -> enum
            if (dstType.IsEnum && IsIntegralType(srcType))
            {
                var backingDstType = Enum.GetUnderlyingType(dstType);
                if (underlyingDstType != null)
                {
                    // if dstType is Nullable<Enum>, convert to enum value
                    return(delegate(object src) { return Enum.ToObject(dstType, src); });
                }
                else if (srcType != backingDstType)
                {
                    return(delegate(object src) { return Convert.ChangeType(src, backingDstType, null); });
                }
            }
            else if (!dstType.IsAssignableFrom(srcType))
            {
                if (dstType.IsEnum && srcType == typeof(string))
                {
                    return(delegate(object src) { return EnumMapper.EnumFromString(dstType, (string)src); });
                }
                else if (dstType == typeof(Guid) && srcType == typeof(string))
                {
                    return(delegate(object src) { return Guid.Parse((string)src); });
                }
                else
                {
                    return(delegate(object src) { return Convert.ChangeType(src, dstType, null); });
                }
            }

            return(null);
        }
コード例 #2
0
ファイル: EnumMapperTest.cs プロジェクト: microjie/PetaPoco
        public void Mapping_ExistingEnum_Ok()
        {
            var expected = FakeEnum.ExistingValue;
            var enumType = expected.GetType();
            var name     = Enum.GetName(enumType, expected);

            var actual = EnumMapper.EnumFromString(enumType, name);

            actual.ShouldBe(expected);
        }
コード例 #3
0
ファイル: PocoData.cs プロジェクト: malsoft-pl/PetaPoco
        private static Func <object, object> GetConverter(IMapper mapper, PocoColumn pc, Type srcType, Type dstType)
        {
            Func <object, object> converter = null;

            // Get converter from the mapper
            if (pc != null)
            {
                converter = mapper.GetFromDbConverter(pc.PropertyInfo, srcType);
                if (converter != null)
                {
                    return(converter);
                }
            }

            // Standard DateTime->Utc mapper
            if (pc != null && pc.ForceToUtc && srcType == typeof(DateTime) && (dstType == typeof(DateTime) || dstType == typeof(DateTime?)))
            {
                return(delegate(object src) { return new DateTime(((DateTime)src).Ticks, DateTimeKind.Utc); });
            }

            // Forced type conversion including integral types -> enum
            if (dstType.IsEnum && IsIntegralType(srcType))
            {
                if (srcType != typeof(int))
                {
                    return(delegate(object src) { return Convert.ChangeType(src, typeof(int), null); });
                }
            }
            else if (!dstType.IsAssignableFrom(srcType))
            {
                if (dstType.IsEnum && srcType == typeof(string))
                {
                    return(delegate(object src) { return EnumMapper.EnumFromString(dstType, (string)src); });
                }
                else if (dstType == typeof(Guid) && srcType == typeof(string))
                {
                    return(delegate(object src) { return Guid.Parse((string)src); });
                }
                else
                {
                    return(delegate(object src) { return Convert.ChangeType(src, dstType, null); });
                }
            }

            return(null);
        }
コード例 #4
0
ファイル: EnumMapperTest.cs プロジェクト: microjie/PetaPoco
        public void Mapping_NonExistingEnum_ThrowsMeaningfulException()
        {
            var enumType        = typeof(FakeEnum);
            var nonExistentName = "ThisValueDoesNotExistOnAnyEnum";

            try
            {
                EnumMapper.EnumFromString(enumType, nonExistentName);
            }
            catch (KeyNotFoundException ex)
            {
                var message = ex.Message;
                message.ShouldContain(nonExistentName, "missing value to convert");
                message.ShouldContain(enumType.Name, "missing Enum to convert into");
                return;
            }

            Assert.False(true, "Expedted InvalidOperationException");
        }