private static Expression WrapInTryCatch(Expression mappingBlock, IMemberMapperData mapperData) { var configuredCallback = mapperData.MapperContext.UserConfigurations.GetExceptionCallbackOrNull(mapperData); var exceptionVariable = Parameters.Create <Exception>("ex"); Expression catchBody; if (configuredCallback != null) { var callbackActionType = configuredCallback.Type.GetGenericTypeArguments()[0]; Type[] contextTypes; Expression contextAccess; if (callbackActionType.IsGenericType()) { contextTypes = callbackActionType.GetGenericTypeArguments(); contextAccess = mapperData.GetAppropriateTypedMappingContextAccess(contextTypes); } else { contextTypes = new[] { mapperData.SourceType, mapperData.TargetType }; contextAccess = mapperData.MappingDataObject; } var exceptionContextCreateMethod = ObjectMappingExceptionData .CreateMethod .MakeGenericMethod(contextTypes); var exceptionContextCreateCall = Expression.Call( exceptionContextCreateMethod, contextAccess, exceptionVariable); var callbackInvocation = Expression.Invoke(configuredCallback, exceptionContextCreateCall); var returnDefault = mappingBlock.Type.ToDefaultExpression(); catchBody = Expression.Block(callbackInvocation, returnDefault); } else { catchBody = Expression.Throw( MappingException.GetFactoryMethodCall(mapperData, exceptionVariable), mappingBlock.Type); } var catchBlock = Expression.Catch(exceptionVariable, catchBody); return(Expression.TryCatch(mappingBlock, catchBlock)); }
public static TryExpression WrapInTryCatch(this Expression mapping, IMemberMapperData mapperData) { var configuredCallback = mapperData.MapperContext.UserConfigurations.GetExceptionCallbackOrNull(mapperData); var exceptionVariable = Parameters.Create <Exception>("ex"); if (configuredCallback == null) { var catchBody = Expression.Throw( MappingException.GetFactoryMethodCall(mapperData, exceptionVariable), mapping.Type); return(CreateTryCatch(mapping, exceptionVariable, catchBody)); } var configuredCatchBody = configuredCallback .ToCatchBody(exceptionVariable, mapping.Type, mapperData); return(CreateTryCatch(mapping, exceptionVariable, configuredCatchBody)); }