Example #1
0
        public MapperConfiguration Register <TSource, TDestination, TPropertyType>(
            Expression <Func <TSource, TPropertyType> > sourcePropertyAccessor,
            Expression <Func <TDestination, TPropertyType> > destinationPropertyAccessor) where TDestination : new()
        {
            if (sourcePropertyAccessor == null)
            {
                throw new ArgumentNullException(nameof(sourcePropertyAccessor));
            }
            if (destinationPropertyAccessor == null)
            {
                throw new ArgumentNullException(nameof(destinationPropertyAccessor));
            }

            PropertyInfo sourceProperty      = GetPropertyInfo(sourcePropertyAccessor);
            PropertyInfo destinationProperty = GetPropertyInfo(destinationPropertyAccessor);

            if (!destinationProperty.CanWrite)
            {
                throw new ArgumentException("Destination property doesn't have setter.");
            }

            Type sourceType      = sourceProperty.PropertyType;
            Type destinationType = destinationProperty.PropertyType;

            if (!TypesHelper.CanAssign(sourceType, destinationType))
            {
                throw new ArgumentException("Incompatible types.");
            }

            var mappingTypesPair = new MappingTypesPair
            {
                Source      = typeof(TSource),
                Destination = typeof(TDestination)
            };

            var newMapping = new MappingPropertiesPair
            {
                SourceProperty      = sourceProperty,
                DestinationProperty = destinationProperty
            };

            List <MappingPropertiesPair> registeredMappings;

            if (!_configuration.TryGetValue(mappingTypesPair, out registeredMappings))
            {
                registeredMappings = new List <MappingPropertiesPair>();
                _configuration[mappingTypesPair] = registeredMappings;
            }

            registeredMappings.Add(newMapping);

            return(this);
        }
Example #2
0
        // Internals

        protected bool Equals(MappingPropertiesPair other)
        {
            return(Equals(SourceProperty, other.SourceProperty) && Equals(DestinationProperty, other.DestinationProperty));
        }