private static Expression ApplySourceFilterIfAppropriate(
            Expression elementPopulation,
            IPopulationLoopData loopData,
            EnumerablePopulationBuilder builder)
        {
            if (!builder.MapperData.MapperContext.UserConfigurations.HasSourceValueFilters)
            {
                return(elementPopulation);
            }

            var sourceElement = loopData.GetSourceElementValue();

            var sourceValueFilters = builder.MapperData
                                     .GetSourceValueFilters(sourceElement.Type);

            if (sourceValueFilters.None())
            {
                return(elementPopulation);
            }

            var sourceFilterConditions = sourceValueFilters
                                         .GetFilterConditionsOrNull(sourceElement, builder.MapperData);

            return((sourceFilterConditions != null)
                ? Expression.IfThen(sourceFilterConditions, elementPopulation)
                : elementPopulation);
        }
        private void AddDerivedSourceTypePopulations(
            IPopulationLoopData loopData,
            QualifiedMember dictionaryEntryMember,
            IObjectMappingData mappingData,
            IEnumerable <Type> derivedSourceTypes,
            ICollection <ParameterExpression> typedVariables,
            ICollection <Expression> mappingExpressions)
        {
            var sourceElement  = loopData.GetSourceElementValue();
            var mapNextElement = Expression.Continue(loopData.ContinueLoopTarget);

            var orderedDerivedSourceTypes = derivedSourceTypes
                                            .OrderBy(t => t, TypeComparer.MostToLeastDerived);

            foreach (var derivedSourceType in orderedDerivedSourceTypes)
            {
                var derivedSourceCheck      = new DerivedSourceTypeCheck(derivedSourceType);
                var typedVariableAssignment = derivedSourceCheck.GetTypedVariableAssignment(sourceElement);

                typedVariables.Add(derivedSourceCheck.TypedVariable);
                mappingExpressions.Add(typedVariableAssignment);

                var derivedTypeMapping    = GetDerivedTypeMapping(derivedSourceCheck, mappingData);
                var derivedTypePopulation = GetPopulation(derivedTypeMapping, dictionaryEntryMember, mappingData);
                var incrementCounter      = _wrappedBuilder.GetCounterIncrement();
                var derivedMappingBlock   = Expression.Block(derivedTypePopulation, incrementCounter, mapNextElement);
                var ifDerivedTypeReturn   = Expression.IfThen(derivedSourceCheck.TypeCheck, derivedMappingBlock);

                mappingExpressions.Add(ifDerivedTypeReturn);
            }
        }
        private void InsertSourceElementNullCheck(
            IPopulationLoopData loopData,
            DictionaryTargetMember dictionaryEntryMember,
            IMemberMapperData mapperData,
            IList <Expression> mappingExpressions)
        {
            var sourceElement = loopData.GetSourceElementValue();

            if (sourceElement.Type.CannotBeNull())
            {
                return;
            }

            loopData.NeedsContinueTarget = true;

            var sourceElementIsNull = sourceElement.GetIsDefaultComparison();

            var nullTargetValue = dictionaryEntryMember.ValueType.ToDefaultExpression();
            var addNullEntry    = dictionaryEntryMember.GetPopulation(nullTargetValue, mapperData);

            var incrementCounter = _wrappedBuilder.GetCounterIncrement();
            var continueLoop     = Expression.Continue(loopData.ContinueLoopTarget);

            var nullEntryActions = Expression.Block(addNullEntry, incrementCounter, continueLoop);

            var ifNullContinue = Expression.IfThen(sourceElementIsNull, nullEntryActions);

            mappingExpressions.Insert(0, ifNullContinue);
        }
Esempio n. 4
0
        private static void InsertSourceElementNullCheck(IPopulationLoopData loopData, IList <Expression> mappingExpressions)
        {
            var sourceElement = loopData.GetSourceElementValue();

            if (sourceElement.Type.CannotBeNull())
            {
                return;
            }

            loopData.NeedsContinueTarget = true;

            var sourceElementIsNull = sourceElement.GetIsDefaultComparison();
            var continueLoop        = Expression.Continue(loopData.ContinueLoopTarget);
            var ifNullContinue      = Expression.IfThen(sourceElementIsNull, continueLoop);

            mappingExpressions.Insert(0, ifNullContinue);
        }