private static void AddDeclaredSourceTypeMappings(
            IEnumerable <DerivedTypePair> derivedTypePairs,
            IObjectMappingData declaredTypeMappingData,
            ICollection <Expression> derivedTypeMappingExpressions,
            out bool declaredTypeHasUnconditionalTypePair)
        {
            var declaredTypeMapperData = declaredTypeMappingData.MapperData;

            derivedTypePairs = derivedTypePairs
                               .OrderBy(tp => tp.DerivedSourceType, TypeComparer.MostToLeastDerived);

            foreach (var derivedTypePair in derivedTypePairs)
            {
                var condition = GetTypePairCondition(derivedTypePair, declaredTypeMapperData);

                var sourceValue = GetDerivedTypeSourceValue(
                    derivedTypePair,
                    declaredTypeMappingData,
                    out var sourceValueCondition);

                var derivedTypeMapping = DerivedMappingFactory.GetDerivedTypeMapping(
                    declaredTypeMappingData,
                    sourceValue,
                    derivedTypePair.DerivedTargetType);

                if (sourceValueCondition != null)
                {
                    derivedTypeMapping = Expression.Condition(
                        sourceValueCondition,
                        derivedTypeMapping,
                        derivedTypeMapping.Type.ToDefaultExpression());
                }

                var returnMappingResult = Expression.Return(declaredTypeMapperData.ReturnLabelTarget, derivedTypeMapping);

                declaredTypeHasUnconditionalTypePair = (condition == null);

                if (declaredTypeHasUnconditionalTypePair)
                {
                    derivedTypeMappingExpressions.Add(returnMappingResult);
                    return;
                }

                var ifConditionThenMap = Expression.IfThen(condition, returnMappingResult);

                derivedTypeMappingExpressions.Add(ifConditionThenMap);
            }

            declaredTypeHasUnconditionalTypePair = false;
        }
예제 #2
0
        private static Expression GetReturnMappingResultExpression(
            IObjectMappingData mappingData,
            Expression sourceValue,
            Type targetType)
        {
            var mapping = DerivedMappingFactory.GetDerivedTypeMapping(mappingData, sourceValue, targetType);

            if (mapping == Constants.EmptyExpression)
            {
                return(Constants.EmptyExpression);
            }

            var returnMappingResult = Expression.Return(mappingData.MapperData.ReturnLabelTarget, mapping);

            return(returnMappingResult);
        }