private void FindAndSetFlattenedMatches(IObjectMapperContext mapperContext, string prefix, PropertyInfo property, object obj, List <PropertyInfo> destinationProperties, object destination)
        {
            var propertyValue = property.GetValue(obj, null);

            if (propertyValue == null)
            {
                return;
            }

            // look for a matching destination property
            var destinationProperty = destinationProperties.FirstOrDefault(p => p.Name == prefix + property.Name && p.CanWrite && p.GetSetMethod() != null);

            if (destinationProperty != null)
            {
                SetDestinationPropertyValue(mapperContext, property, propertyValue, destinationProperty, destination);
            }

            var propertyValueType = propertyValue.GetType();

            // make sure we can inspect this further
            if (propertyValueType.IsValueType || propertyValueType.IsPrimitive || propertyValueType == typeof(String))
            {
                return;
            }

            var subProperties = ReflectionUtils.GetProperties(propertyValueType);

            foreach (var subProperty in subProperties)
            {
                if (subProperty.CanRead && subProperty.GetGetMethod() != null)
                {
                    FindAndSetFlattenedMatches(mapperContext, prefix + property.Name, subProperty, propertyValue, destinationProperties, destination);
                }
            }
        }
Exemple #2
0
        protected override void ApplyMap(IObjectMapperContext mapperContext, object source, object destination)
        {
            var sourceProperties      = ReflectionUtils.GetProperties(source);
            var destinationProperties = ReflectionUtils.GetProperties(destination);

            foreach (var sourceProperty in sourceProperties)
            {
                // make sure this source property is something we can read and isn't excluded
                if (!IsSourcePropertyValid(sourceProperty))
                {
                    continue;
                }

                // find a matching destination property
                var destinationProperty = FindDestinationPropertyWithMatchingName(sourceProperty, destinationProperties);
                if (destinationProperty == null)
                {
                    HandleNoMatchingPropertyName(mapperContext, sourceProperty, source, destination);
                    continue;
                }

                // we have a matching name but make sure we can write to it
                if (!destinationProperty.CanWrite || destinationProperty.GetSetMethod() == null)
                {
                    continue;
                }

                var sourcePropertyValue = sourceProperty.GetValue(source, null);
                SetDestinationPropertyValue(mapperContext, sourceProperty, sourcePropertyValue, destinationProperty, destination);
            }
        }
        protected override void HandleNoMatchingPropertyName(IObjectMapperContext mapperContext, PropertyInfo sourceProperty, object source,
                                                             object destination)
        {
            // if there's not a matching name and this property isn't something we can further
            // inspect then there's nothing to do
            if (sourceProperty.PropertyType.IsValueType || sourceProperty.PropertyType.IsPrimitive || sourceProperty.PropertyType == typeof(String))
            {
                return;
            }

            var sourcePropertyValue = sourceProperty.GetValue(source, null);

            // if the source is null we can't inspect it further
            if (sourcePropertyValue == null)
            {
                return;
            }

            // find all the flattened matches and set them
            var subProperties         = ReflectionUtils.GetProperties(sourcePropertyValue);
            var destinationProperties = ReflectionUtils.GetProperties(destination).ToList();

            foreach (var subProperty in subProperties)
            {
                FindAndSetFlattenedMatches(mapperContext, sourceProperty.Name, subProperty, sourcePropertyValue, destinationProperties, destination);
            }
        }
Exemple #4
0
        /// <summary>
        /// Handles the properties with different types.
        /// </summary>
        /// <param name="mapperContext">The mapper context.</param>
        /// <param name="sourceProperty">The source property.</param>
        /// <param name="sourcePropertyValue">The source property value.</param>
        /// <param name="destinationProperty">The destination property.</param>
        /// <param name="destination">The destination.</param>
        protected virtual void HandlePropertiesWithDifferentTypes(IObjectMapperContext mapperContext, PropertyInfo sourceProperty, object sourcePropertyValue, PropertyInfo destinationProperty, object destination)
        {
            // find a converter that can convert between types
            var typeConverter = mapperContext.TypeConverters.FirstOrDefault(
                t => t.CanConvert(sourceProperty.PropertyType, destinationProperty.PropertyType));

            if (typeConverter != null)
            {
                var convertedValue = typeConverter.Convert(sourcePropertyValue);
                destinationProperty.SetValue(destination, convertedValue, null);
                return;
            }

            // see if there's an objectmap that can convert between types
            var objectMap = mapperContext.Maps.FirstOrDefault(m =>
                                                              m.SourceType == sourceProperty.PropertyType &&
                                                              m.DestinationType == destinationProperty.PropertyType);

            if (objectMap == null)
            {
                return;
            }

            destinationProperty.SetValue(destination, objectMap.Map(mapperContext, sourcePropertyValue), null);
        }
            protected override PersonDto MapObject(IObjectMapperContext mapperContext, PersonModel source)
            {
                var dto = mapperContext.Map <PersonModel, PersonDto>(source);

                dto.Name = Name;
                return(dto);
            }
Exemple #6
0
 /// <summary>
 /// Sets the destination property value.
 /// </summary>
 /// <param name="mapperContext">The mapper context.</param>
 /// <param name="sourceProperty">The source property.</param>
 /// <param name="sourcePropertyValue">The source property value.</param>
 /// <param name="destinationProperty">The destination property.</param>
 /// <param name="destination">The destination object to set the value on.</param>
 protected virtual void SetDestinationPropertyValue(IObjectMapperContext mapperContext, PropertyInfo sourceProperty,
                                                    object sourcePropertyValue, PropertyInfo destinationProperty, object destination)
 {
     // if the types match then just set the value it
     if (sourceProperty.PropertyType == destinationProperty.PropertyType)
     {
         destinationProperty.SetValue(destination, sourcePropertyValue, null);
     }
     else
     {
         HandlePropertiesWithDifferentTypes(mapperContext, sourceProperty, sourcePropertyValue, destinationProperty, destination);
     }
 }
Exemple #7
0
        protected override void HandleNoMatchingPropertyName(IObjectMapperContext mapperContext, PropertyInfo sourceProperty, object source, object destination)
        {
            var propertyPath = FindUnFlattenedMatch(sourceProperty.Name, destination);

            if (propertyPath == null)
            {
                return;
            }

            object tempObj             = destination;
            object sourcePropertyValue = sourceProperty.GetValue(source, null);

            // walk the path of properties up to the last one
            for (int i = 0; i < propertyPath.Count; i++)
            {
                // we're on the last property
                if (i == propertyPath.Count - 1)
                {
                    break;
                }

                var propertyInfo = propertyPath[i];
                var value        = propertyInfo.GetValue(tempObj, null);

                // the value could be null if not created yet - if so create it and set it
                if (value == null)
                {
                    value = Activator.CreateInstance(propertyInfo.PropertyType);
                    propertyInfo.SetValue(tempObj, value, null);
                }

                tempObj = value;
            }

            // set the value on final property
            var destinationProperty = propertyPath.Last();

            SetDestinationPropertyValue(mapperContext, sourceProperty, sourcePropertyValue, destinationProperty, tempObj);
        }
Exemple #8
0
 public abstract object Map(IObjectMapperContext objectMapper, object source);
 public object Map(IObjectMapperContext mapperContext, object source, object destination)
 {
     ApplyMap(mapperContext, source, destination);
     return(destination);
 }
 protected abstract void ApplyMap(IObjectMapperContext mapperContext, object source, object destination);
Exemple #11
0
 /// <summary>
 /// Handles the case when the property name does not have a match in the destination.
 /// </summary>
 /// <param name="mapperContext">The mapper context.</param>
 /// <param name="sourceProperty">The source property.</param>
 /// <param name="source">The source.</param>
 /// <param name="destination">The destination.</param>
 protected virtual void HandleNoMatchingPropertyName(IObjectMapperContext mapperContext, PropertyInfo sourceProperty, object source, object destination)
 {
 }
Exemple #12
0
 protected override TDestination MapObject(IObjectMapperContext objectMapper, TSource source)
 {
     return(mappingFunc.Invoke(objectMapper, source));
 }