private static Expression GetCheckedTryCatch( TryExpression tryCatchValue, Expression keyedAccess, Expression checkedAccess, ParameterExpression existingValue) { var existingValueOrDefault = Expression.Condition( checkedAccess, existingValue, existingValue.Type.ToDefaultExpression()); var replacements = new ExpressionReplacementDictionary(1) { [keyedAccess] = existingValueOrDefault }; var updatedCatchHandlers = tryCatchValue .Handlers .ProjectToArray(handler => handler.Update( handler.Variable, handler.Filter.Replace(replacements), handler.Body.Replace(replacements))); var updatedTryCatch = tryCatchValue.Update( tryCatchValue.Body, updatedCatchHandlers, tryCatchValue.Finally, tryCatchValue.Fault); return(Expression.Block(new[] { existingValue }, updatedTryCatch)); }
private static Expression GetDirectAccessMapping( IObjectMappingData mappingData, MappingValues mappingValues, MethodInfo createMethod, Expression[] createMethodCallArguments) { var mapperData = mappingData.MapperData; var mapping = mappingData.Mapper.MappingLambda.Body; var useLocalSourceValueVariable = UseLocalSourceValueVariable(mappingValues.SourceValue, mapping, mapperData); Expression sourceValue, sourceValueVariableValue = null; if (useLocalSourceValueVariable) { var sourceValueVariableName = GetSourceValueVariableName(mapperData, mappingValues.SourceValue.Type); sourceValue = Expression.Variable(mappingValues.SourceValue.Type, sourceValueVariableName); sourceValueVariableValue = mappingValues.SourceValue; } else { sourceValue = mappingValues.SourceValue; } var replacementsByTarget = new ExpressionReplacementDictionary { [mapperData.SourceObject] = sourceValue, [mapperData.TargetObject] = mappingValues.TargetValue, [mapperData.EnumerableIndex] = mappingValues.EnumerableIndex.GetConversionTo(mapperData.EnumerableIndex.Type) }; var directAccessMapping = mapping.Replace(replacementsByTarget); var createInlineMappingDataCall = GetCreateMappingDataCall( createMethod, mapperData, createMethodCallArguments); directAccessMapping = directAccessMapping.Replace( mapperData.MappingDataObject, createInlineMappingDataCall); return(useLocalSourceValueVariable ? UseLocalSourceValueVariable((ParameterExpression)sourceValue, sourceValueVariableValue, directAccessMapping) : directAccessMapping); }
private static Expression GetDirectAccessMapping( Expression mapping, IMemberMapperData mapperData, MappingValues mappingValues, Expression createMappingDataCall) { var useLocalSourceValueVariable = ShouldUseLocalSourceValueVariable(mappingValues.SourceValue, mapping, mapperData); Expression sourceValue, sourceValueVariableValue; if (useLocalSourceValueVariable) { var sourceValueVariableName = mappingValues.SourceValue.Type.GetSourceValueVariableName(); sourceValue = Expression.Variable(mappingValues.SourceValue.Type, sourceValueVariableName); sourceValueVariableValue = mappingValues.SourceValue; } else { sourceValue = mappingValues.SourceValue; sourceValueVariableValue = null; } var replacementsByTarget = new ExpressionReplacementDictionary(3) { [mapperData.SourceObject] = sourceValue, [mapperData.TargetObject] = mappingValues.TargetValue, [mapperData.EnumerableIndex] = mappingValues.EnumerableIndex.GetConversionTo(mapperData.EnumerableIndex.Type) }; mapping = mapping .Replace(replacementsByTarget) .Replace(mapperData.MappingDataObject, createMappingDataCall); return(useLocalSourceValueVariable ? UseLocalValueVariable( (ParameterExpression)sourceValue, sourceValueVariableValue, mapping, mapperData) : mapping); }
private static Expression SwapForContextParameter(SwapArgs swapArgs) { var contextParameter = swapArgs.Lambda.Parameters[0]; var contextType = contextParameter.Type; if (swapArgs.MapperData.MappingDataObject.Type.IsAssignableTo(contextType)) { return(swapArgs.Lambda.ReplaceParameterWith(swapArgs.MapperData.MappingDataObject)); } var contextInfo = GetAppropriateMappingContext(swapArgs); if (swapArgs.Lambda.Body.NodeType == ExpressionType.Invoke) { return(GetInvocationContextArgument(contextInfo, swapArgs.Lambda)); } var contextTypes = contextType.GetGenericTypeArguments(); var memberContextType = IsCallbackContext(contextTypes) ? contextType : contextType.GetAllInterfaces().First(); var sourceProperty = memberContextType.GetPublicInstanceProperty(RootSourceMemberName); var targetProperty = memberContextType.GetPublicInstanceProperty(RootTargetMemberName); var indexProperty = memberContextType.GetPublicInstanceProperty("EnumerableIndex"); var parentProperty = memberContextType.GetPublicInstanceProperty("Parent"); var replacementsByTarget = new ExpressionReplacementDictionary(5) { [Expression.Property(contextParameter, sourceProperty)] = contextInfo.SourceAccess, [Expression.Property(contextParameter, targetProperty)] = contextInfo.TargetAccess, [Expression.Property(contextParameter, indexProperty)] = contextInfo.Index, [Expression.Property(contextParameter, parentProperty)] = contextInfo.Parent }; if (IsObjectCreationContext(contextTypes)) { replacementsByTarget.Add( Expression.Property(contextParameter, "CreatedObject"), contextInfo.CreatedObject); } return(swapArgs.Lambda.Body.Replace(replacementsByTarget)); }
private Expression GetCheckedValueOrNull(Expression value, Expression keyedAccess, IMemberMapperData mapperData) { if (HasSimpleEntries) { return(null); } if (mapperData.SourceMember.IsEnumerable) { return(value.GetConversionTo(ValueType)); } if ((value.NodeType != Block) && (value.NodeType != Try)) { return(null); } var checkedAccess = GetAccessChecked(mapperData); var existingValue = checkedAccess.Variables.First(); if (value.NodeType != Block) { return(GetCheckedTryCatch((TryExpression)value, keyedAccess, checkedAccess, existingValue)); } var replacements = new ExpressionReplacementDictionary(1) { [keyedAccess] = existingValue }; var checkedValue = ((BlockExpression)value).Replace(replacements); return(checkedValue.Update( checkedValue.Variables.Append(existingValue), checkedValue.Expressions.Prepend(checkedAccess.Expressions.First()))); }