Beispiel #1
0
        public static Type GetEnumerationType(Type enumType)
        {
            if (PrimitiveExtensions.IsNullableType(enumType))
            {
                enumType = enumType.GetGenericArguments()[0];
            }

            if (!TypeExtensions.IsEnum(enumType))
            {
                return(null);
            }

            return(enumType);
        }
        private static MethodInfo GetImplicitConversionOperator(ResolutionContext context)
        {
            var destinationType = context.DestinationType;

            if (PrimitiveExtensions.IsNullableType(destinationType))
            {
                destinationType = PrimitiveExtensions.GetTypeOfNullable(destinationType);
            }
            var sourceTypeMethod = TypeExtensions.GetDeclaredMethods(context.SourceType)
                                   .FirstOrDefault(
                mi => mi.IsPublic && mi.IsStatic && mi.Name == "op_Implicit" && mi.ReturnType == destinationType);

            return(sourceTypeMethod ?? destinationType.GetMethod("op_Implicit", new[] { context.SourceType }));
        }
Beispiel #3
0
        bool IMappingEngineRunner.ShouldMapSourceValueAsNull(ResolutionContext context)
        {
            if (TypeExtensions.IsValueType(context.DestinationType) &&
                !PrimitiveExtensions.IsNullableType(context.DestinationType))
            {
                return(false);
            }

            var typeMap = context.GetContextTypeMap();

            if (typeMap != null)
            {
                return(ConfigurationProvider.GetProfileConfiguration(typeMap.Profile).AllowNullDestinationValues);
            }

            return(ConfigurationProvider.AllowNullDestinationValues);
        }
Beispiel #4
0
        private void DryRunTypeMap(ICollection <TypeMap> typeMapsChecked, ResolutionContext context)
        {
            if (context.TypeMap != null)
            {
                typeMapsChecked.Add(context.TypeMap);
            }

            var mapperToUse = GetMappers().FirstOrDefault(mapper => mapper.IsMatch(context));

            if (mapperToUse == null && PrimitiveExtensions.IsNullableType(context.SourceType))
            {
                var nullableContext = context.CreateValueContext(null, Nullable.GetUnderlyingType(context.SourceType));

                mapperToUse = GetMappers().FirstOrDefault(mapper => mapper.IsMatch(nullableContext));
            }

            if (mapperToUse == null)
            {
                throw new AutoMapperConfigurationException(context);
            }

            if (mapperToUse is TypeMapMapper)
            {
                foreach (var propertyMap in context.TypeMap.GetPropertyMaps())
                {
                    if (!propertyMap.IsIgnored())
                    {
                        var lastResolver =
                            propertyMap.GetSourceValueResolvers().OfType <IMemberResolver>().LastOrDefault();

                        if (lastResolver != null)
                        {
                            var sourceType      = lastResolver.MemberType;
                            var destinationType = propertyMap.DestinationProperty.MemberType;
                            var memberTypeMap   = ((IConfigurationProvider)this).ResolveTypeMap(sourceType,
                                                                                                destinationType);

                            if (typeMapsChecked.Any(typeMap => Equals(typeMap, memberTypeMap)))
                            {
                                continue;
                            }

                            var memberContext = context.CreateMemberContext(memberTypeMap, null, null, sourceType,
                                                                            propertyMap);

                            DryRunTypeMap(typeMapsChecked, memberContext);
                        }
                    }
                }
            }
            else if (mapperToUse is ArrayMapper || mapperToUse is EnumerableMapper || mapperToUse is CollectionMapper)
            {
                var sourceElementType = TypeHelper.GetElementType(context.SourceType);
                var destElementType   = TypeHelper.GetElementType(context.DestinationType);
                var itemTypeMap       = ((IConfigurationProvider)this).ResolveTypeMap(sourceElementType, destElementType);

                if (typeMapsChecked.Any(typeMap => Equals(typeMap, itemTypeMap)))
                {
                    return;
                }

                var memberContext = context.CreateElementContext(itemTypeMap, null, sourceElementType, destElementType,
                                                                 0);

                DryRunTypeMap(typeMapsChecked, memberContext);
            }
        }
Beispiel #5
0
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            var toEnum              = false;
            var enumSourceType      = TypeHelper.GetEnumerationType(context.SourceType);
            var enumDestinationType = TypeHelper.GetEnumerationType(context.DestinationType);

            if (EnumToStringMapping(context, ref toEnum))
            {
                if (context.SourceValue == null)
                {
                    return(mapper.CreateObject(context));
                }

                if (toEnum)
                {
                    var stringValue = context.SourceValue.ToString();
                    if (string.IsNullOrEmpty(stringValue))
                    {
                        return(mapper.CreateObject(context));
                    }

                    return(Enum.Parse(enumDestinationType, stringValue, true));
                }
                return(Enum.GetName(enumSourceType, context.SourceValue));
            }
            if (EnumToEnumMapping(context))
            {
                if (context.SourceValue == null)
                {
                    if (mapper.ShouldMapSourceValueAsNull(context) &&
                        PrimitiveExtensions.IsNullableType(context.DestinationType))
                    {
                        return(null);
                    }

                    return(mapper.CreateObject(context));
                }

                if (!Enum.IsDefined(enumSourceType, context.SourceValue))
                {
                    return(Enum.ToObject(enumDestinationType, context.SourceValue));
                }

                if (FeatureDetector.IsEnumGetNamesSupported)
                {
                    var enumValueMapper = EnumNameValueMapperFactory.Create();

                    if (enumValueMapper.IsMatch(enumDestinationType, context.SourceValue.ToString()))
                    {
                        return(enumValueMapper.Convert(enumSourceType, enumDestinationType, context));
                    }
                }

                return(Enum.Parse(enumDestinationType, Enum.GetName(enumSourceType, context.SourceValue), true));
            }
            if (EnumToUnderlyingTypeMapping(context, ref toEnum))
            {
                if (toEnum && context.SourceValue != null)
                {
                    return(Enum.Parse(enumDestinationType, context.SourceValue.ToString(), true));
                }

                if (EnumToNullableTypeMapping(context))
                {
                    return(ConvertEnumToNullableType(context));
                }

                return(Convert.ChangeType(context.SourceValue, context.DestinationType, null));
            }
            return(null);
        }
Beispiel #6
0
 public bool IsMatch(ResolutionContext context)
 {
     return(PrimitiveExtensions.IsNullableType(context.DestinationType));
 }
 public bool IsMatch(PropertyMap propertyMap, TypeMap propertyTypeMap, ExpressionResolutionResult result)
 {
     return(PrimitiveExtensions.IsNullableType(propertyMap.DestinationPropertyType) &&
            !PrimitiveExtensions.IsNullableType(result.Type));
 }
Beispiel #8
0
 private static bool BothAreNonNullable(Expression node, Expression newLeft)
 {
     return(!PrimitiveExtensions.IsNullableType(node.Type) &&
            !PrimitiveExtensions.IsNullableType(newLeft.Type));
 }
Beispiel #9
0
 private static bool GoingFromNonNullableToNullable(Expression node, Expression newLeft)
 {
     return(!PrimitiveExtensions.IsNullableType(node.Type) &&
            PrimitiveExtensions.IsNullableType(newLeft.Type));
 }