private LambdaExpression GenerateObjectMapperExpression(MapRequest mapRequest, IObjectMapper mapperToUse) { var destinationType = mapRequest.RequestedTypes.DestinationType; var source = Parameter(mapRequest.RequestedTypes.SourceType, "source"); var destination = Parameter(destinationType, "mapperDestination"); var context = Parameter(typeof(ResolutionContext), "context"); Expression fullExpression; if (mapperToUse == null) { var message = Constant("Missing type map configuration or unsupported mapping."); fullExpression = Block(Throw(New(ExceptionConstructor, message, Constant(null, typeof(Exception)), Constant(mapRequest.RequestedTypes))), Default(destinationType)); } else { var map = mapperToUse.MapExpression(this, Configuration, mapRequest.PropertyMap, ToType(source, mapRequest.RuntimeTypes.SourceType), ToType(destination, mapRequest.RuntimeTypes.DestinationType), context); var exception = Parameter(typeof(Exception), "ex"); fullExpression = TryCatch(ToType(map, destinationType), MakeCatchBlock(typeof(Exception), exception, Block( Throw(New(ExceptionConstructor, Constant("Error mapping types."), exception, Constant(mapRequest.RequestedTypes))), Default(destination.Type)), null)); } var profileMap = mapRequest.PropertyMap?.TypeMap?.Profile ?? Configuration; var nullCheckSource = NullCheckSource(profileMap, source, destination, fullExpression, mapRequest.PropertyMap); return(Lambda(nullCheckSource, source, destination, context)); }
private static LambdaExpression GenerateObjectMapperExpression(MapRequest mapRequest, IObjectMapper mapperToUse, MapperConfiguration mapperConfiguration) { var destinationType = mapRequest.RequestedTypes.DestinationType; var source = Parameter(mapRequest.RequestedTypes.SourceType, "source"); var destination = Parameter(destinationType, "destination"); var context = Parameter(typeof(ResolutionContext), "context"); var ctor = ((NewExpression)ResolutionContextCtor.Body).Constructor; LambdaExpression fullExpression; if (mapperToUse == null) { var message = Constant("Missing type map configuration or unsupported mapping."); fullExpression = Lambda(Block(Throw(New(ctor, message, Constant(null), Constant(mapRequest.RequestedTypes))), Default(destinationType)), source, destination, context); } else { var map = mapperToUse.MapExpression(mapperConfiguration._typeMapRegistry, mapperConfiguration, null, ToType(source, mapRequest.RuntimeTypes.SourceType), destination, context); var mapToDestination = Lambda(ToType(map, destinationType), source, destination, context); fullExpression = TryCatch(mapToDestination, source, destination, context, mapRequest.RequestedTypes); } return(fullExpression); }
private static LambdaExpression GenerateObjectMapperExpression(MapRequest mapRequest, IObjectMapper mapperToUse, MapperConfiguration mapperConfiguration) { var destinationType = mapRequest.RequestedTypes.DestinationType; var source = Expression.Parameter(mapRequest.RequestedTypes.SourceType, "source"); var destination = Expression.Parameter(destinationType, "mapperDestination"); var context = Expression.Parameter(typeof(ResolutionContext), "context"); LambdaExpression fullExpression; if (mapperToUse == null) { var message = Expression.Constant("Missing type map configuration or unsupported mapping."); fullExpression = Expression.Lambda(Expression.Block(Expression.Throw(Expression.New(ExceptionConstructor, message, Expression.Constant(null, typeof(Exception)), Expression.Constant(mapRequest.RequestedTypes))), Expression.Default(destinationType)), source, destination, context); } else { var map = mapperToUse.MapExpression(mapperConfiguration.TypeMapRegistry, mapperConfiguration, null, ExpressionExtensions.ToType(source, mapRequest.RuntimeTypes.SourceType), destination, context); var mapToDestination = Expression.Lambda(ExpressionExtensions.ToType(map, destinationType), source, destination, context); fullExpression = TryCatch(mapToDestination, source, destination, context, mapRequest.RequestedTypes); } return(fullExpression); }