public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            if (context.SourceValue == null && !mapper.ShouldMapSourceValueAsNull(context))
            {
                return mapper.CreateObject(context);
            }

            return context.SourceValue;
        }
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            if (context.SourceValue == null && !mapper.ShouldMapSourceValueAsNull(context))
            {
                return(mapper.CreateObject(context));
            }

            return(context.SourceValue);
        }
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            if (context == null) throw new ArgumentNullException("context");
            if (mapper == null) throw new ArgumentNullException("mapper");

            if (context.SourceValue == null && !mapper.ShouldMapSourceValueAsNull(context))
            {
                return mapper.CreateObject(context);
            }

            return context.SourceValue;
        }
Beispiel #4
0
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            bool toEnum              = false;
            Type enumSourceType      = TypeHelper.GetEnumerationType(context.SourceType);
            Type 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) && context.DestinationType.IsNullableType())
                    {
                        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)
                {
                    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 #5
0
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            bool toEnum = false;
            Type enumSourceType = TypeHelper.GetEnumerationType(context.SourceType);
            Type 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) && context.DestinationType.IsNullableType())
                        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 object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            bool toEnum              = false;
            Type enumSourceType      = TypeHelper.GetEnumerationType(context.SourceType);
            Type 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) && context.DestinationType.IsNullableType())
                    {
                        return(null);
                    }

                    return(mapper.CreateObject(context));
                }

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

                if (!Enum.GetNames(enumDestinationType).Contains(context.SourceValue.ToString()))
                {
                    Type underlyingSourceType  = Enum.GetUnderlyingType(enumSourceType);
                    var  underlyingSourceValue = Convert.ChangeType(context.SourceValue, underlyingSourceType);

                    return(Enum.ToObject(context.DestinationType, underlyingSourceValue));
                }

                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 #7
0
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            bool toEnum = false;
            Type enumSourceType = TypeHelper.GetEnumerationType(context.SourceType);
            Type 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) && context.DestinationType.IsNullableType())
                        return null;

                    return mapper.CreateObject(context);
                }

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

            #if !SILVERLIGHT
                if (!Enum.GetNames(enumDestinationType).Contains(context.SourceValue.ToString()))
                {
                    Type underlyingSourceType = Enum.GetUnderlyingType(enumSourceType);
                    var underlyingSourceValue = Convert.ChangeType(context.SourceValue, underlyingSourceType);

                    return Enum.ToObject(context.DestinationType, underlyingSourceValue);
                }
            #endif

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

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

                return Convert.ChangeType(context.SourceValue, context.DestinationType, null);
            }
            return null;
        }