Пример #1
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)
                {
                    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));
                }
                return(Convert.ChangeType(context.SourceValue, context.DestinationType, null));
            }
            return(null);
        }
Пример #2
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)
                {
                    return mapper.CreateObject(context);
                }

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

            #if !SILVERLIGHT && !__ANDROID__
                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);
                }
                return Convert.ChangeType(context.SourceValue, context.DestinationType, null);
            }
            return null;
        }
Пример #3
0
            protected override object GetMappedObject(ResolutionContext context, IMappingEngineRunner mapper)
            {
                var result = mapper.CreateObject(context);

                context.SetResolvedDestinationValue(result);
                return(result);
            }
Пример #4
0
		public object Map(ResolutionContext context, IMappingEngineRunner mapper)
		{
			if (context.SourceValue == null)
			{
				return mapper.CreateObject(context);
			}
			Func<object> converter = GetConverter(context);
			return converter != null ? converter() : null;
		}
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            if (context.SourceValue == null && !mapper.ShouldMapSourceCollectionAsNull(context))
            {
                return mapper.CreateObject(context);
            }

            return context.SourceValue;
        }
Пример #6
0
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            if (context.SourceValue == null && !mapper.ShouldMapSourceCollectionAsNull(context))
            {
                return(mapper.CreateObject(context));
            }

            return(context.SourceValue);
        }
Пример #7
0
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            if (context.SourceValue == null)
            {
                return mapper.CreateObject(context);
            }

            return context.SourceValue;
        }
Пример #8
0
        private object CreateDestinationObject(ResolutionContext context, Type destinationElementType, int count, IMappingEngineRunner mapper)
        {
            var destinationType = context.DestinationType;

            if (!destinationType.IsInterface && !destinationType.IsArray)
            {
                return(mapper.CreateObject(context));
            }
            return(CreateDestinationObjectBase(destinationElementType, count));
        }
Пример #9
0
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            if (context.SourceValue == null)
            {
                return mapper.CreateObject(context);
            }

            TypeConverter typeConverter = GetTypeConverter(context);
            return typeConverter.ConvertTo(context.SourceValue, context.DestinationType);
        }
            protected override object GetMappedObject(ResolutionContext context, IMappingEngineRunner mapper)
            {
                var result = mapper.CreateObject(context);

                if (result == null)
                {
                    throw new InvalidOperationException("Cannot create destination object. " + context);
                }
                return(result);
            }
Пример #11
0
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            if (context.SourceValue == null)
            {
                return(mapper.CreateObject(context));
            }
            Func <object> converter = GetConverter(context);

            return(converter != null?converter() : null);
        }
Пример #12
0
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            if (context.SourceValue == null)
            {
                return(mapper.CreateObject(context));
            }

            TypeConverter typeConverter = GetTypeConverter(context);

            return(typeConverter.ConvertTo(context.SourceValue, context.DestinationType));
        }
Пример #13
0
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            var enumDestType = TypeHelper.GetEnumerationType(context.DestinationType);

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

            return(Enum.Parse(enumDestType, context.SourceValue.ToString(), true));
        }
Пример #14
0
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            var enumDestType = TypeHelper.GetEnumerationType(context.DestinationType);

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

            return Enum.Parse(enumDestType, context.SourceValue.ToString(), true);
        }
Пример #15
0
        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;
        }
Пример #16
0
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            Type enumDestType = TypeHelper.GetEnumerationType(context.DestinationType);

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

            Type enumSourceType = TypeHelper.GetEnumerationType(context.SourceType);

            return Enum.Parse(enumDestType, Enum.GetName(enumSourceType, context.SourceValue));
        }
Пример #17
0
        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)
            {
                return mapper.CreateObject(context);
            }

            TypeConverter typeConverter = GetTypeConverter(context);
            return typeConverter.ConvertTo(context.SourceValue, context.DestinationType);
        }
 public object Map(ResolutionContext context, IMappingEngineRunner mapper)
 {
     var dictionary = (StringDictionary)context.SourceValue;
     object destination = mapper.CreateObject(context);
     var destTypeDetails = new TypeDetails(context.DestinationType, _ => true, _ => true);
     var members = from name in dictionary.Keys join member in destTypeDetails.PublicWriteAccessors on name equals member.Name select member;
     foreach(var member in members)
     {
         object value = ReflectionHelper.Map(member, dictionary[member.Name]);
         member.SetMemberValue(destination, value);
     }
     return destination;
 }
Пример #19
0
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            if (context == null) throw new ArgumentNullException("context");
            if (mapper == null) throw new ArgumentNullException("mapper");

            bool toEnum = false;
            Type enumSourceType = TypeHelper.GetEnumerationType(context.SourceType);
            Type enumDestinationType = TypeHelper.GetEnumerationType(context.DestinationType);

            if (EnumToStringMapping(context, ref toEnum))
            {
                if (toEnum)
                {
                    return Enum.Parse(enumDestinationType, context.SourceValue.ToString(), true);
                }
                return Enum.GetName(enumSourceType, context.SourceValue);
            }
            if (EnumToEnumMapping(context))
            {
                if (context.SourceValue == 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);
                }
                return Convert.ChangeType(context.SourceValue, context.DestinationType, null);
            }
            return null;
        }
Пример #20
0
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            var    dictionary      = (StringDictionary)context.SourceValue;
            object destination     = mapper.CreateObject(context);
            var    destTypeDetails = new TypeDetails(context.DestinationType, _ => true, _ => true);
            var    members         = from name in dictionary.Keys join member in destTypeDetails.PublicWriteAccessors on name equals member.Name select member;

            foreach (var member in members)
            {
                object value = ReflectionHelper.Map(member, dictionary[member.Name]);
                member.SetMemberValue(destination, value);
            }
            return(destination);
        }
Пример #21
0
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            if (context == null) throw new ArgumentNullException("context");
            if (mapper == null) throw new ArgumentNullException("mapper");

            Type enumDestType = TypeHelper.GetEnumerationType(context.DestinationType);

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

            return Enum.Parse(enumDestType, context.SourceValue.ToString(), true);
        }
Пример #22
0
 public object Map(ResolutionContext context, IMappingEngineRunner mapper)
 {
     // If source is int, create an identifiable for the destination.
     // Otherwise, get the Id of the identifiable.
     if (typeof(int) == context.SourceType)
     {
         var identifiable = (IIdentifiable)mapper.CreateObject(context);
         identifiable.Id = (int)context.SourceValue;
         return(identifiable);
     }
     else
     {
         return(((IIdentifiable)context.SourceValue).Id);
     }
 }
Пример #23
0
 public object Map(ResolutionContext context, IMappingEngineRunner mapper)
 {
     var source = context.SourceValue;
     var destination = mapper.CreateObject(context);
     foreach (var member in MembersToMap(source, destination))
     {
         object sourceMemberValue;
         try
         {
             sourceMemberValue = GetSourceMember(member, source);
         }
         catch (RuntimeBinderException)
         {
             continue;
         }
         var destinationMemberValue = ReflectionHelper.Map(member, sourceMemberValue);
         SetDestinationMember(member, destination, destinationMemberValue);
     }
     return destination;
 }
Пример #24
0
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            var source      = context.SourceValue;
            var destination = mapper.CreateObject(context);

            foreach (var member in MembersToMap(source, destination))
            {
                object sourceMemberValue;
                try
                {
                    sourceMemberValue = GetSourceMember(member, source);
                }
                catch (RuntimeBinderException)
                {
                    continue;
                }
                var destinationMemberValue = ReflectionHelper.Map(member, sourceMemberValue);
                SetDestinationMember(member, destination, destinationMemberValue);
            }
            return(destination);
        }
Пример #25
0
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            bool toEnum = false;
            Type enumSourceType = TypeHelper.GetEnumerationType(context.SourceType);

            if (EnumToStringMapping(context, ref toEnum))
            {
                if (toEnum)
                {
                    return Enum.Parse(context.DestinationType, context.SourceValue.ToString(), true);
                }
                return Enum.GetName(enumSourceType, context.SourceValue);
            }
            if (EnumToEnumMapping(context))
            {
                Type enumDestType = TypeHelper.GetEnumerationType(context.DestinationType);

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

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

                return Enum.Parse(enumDestType, Enum.GetName(enumSourceType, context.SourceValue), true);
            }
            if (EnumToUnderlyingTypeMapping(context, ref toEnum))
            {
                if (toEnum)
                {
                    return Enum.Parse(context.DestinationType, context.SourceValue.ToString(), true);
                }
                return Convert.ChangeType(context.SourceValue, context.DestinationType, null);
            }
            return null;
        }
Пример #26
0
 public object Map(ResolutionContext context, IMappingEngineRunner mapper)
 {
     return(context.SourceValue ?? mapper.CreateObject(context));
 }
Пример #27
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;
        }
Пример #28
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);
        }
 protected override object GetMappedObject(ResolutionContext context, IMappingEngineRunner mapper)
 {
     var result = mapper.CreateObject(context);
     if(result == null)
     {
         throw new InvalidOperationException("Cannot create destination object. " + context);
     }
     return result;
 }
 protected override object GetMappedObject(ResolutionContext context, IMappingEngineRunner mapper)
 {
     var result = mapper.CreateObject(context);
     context.SetResolvedDestinationValue(result);
     return result;
 }
 protected override object GetMappedObject(ResolutionContext context, IMappingEngineRunner mapper)
 {
     return mapper.CreateObject(context);
 }
Пример #32
0
 protected override object GetMappedObject(ResolutionContext context, IMappingEngineRunner mapper)
 {
     return(mapper.CreateObject(context));
 }