Exemple #1
0
        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.GetGenericArguments()[0];

                Type[]     contextTypes;
                Expression contextAccess;

                if (callbackActionType.IsGenericType())
                {
                    contextTypes  = callbackActionType.GetGenericArguments();
                    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      = Expression.Default(mappingBlock.Type);
                catchBody = Expression.Block(callbackInvocation, returnDefault);
            }
            else
            {
                var exceptionFactoryMethod = MappingException.FactoryMethod
                                             .MakeGenericMethod(mapperData.SourceType, mapperData.TargetType);

                var mappingExceptionCreation = Expression.Call(
                    exceptionFactoryMethod,
                    mapperData.MappingDataObject,
                    exceptionVariable);

                catchBody = Expression.Throw(mappingExceptionCreation, mappingBlock.Type);
            }

            var catchBlock = Expression.Catch(exceptionVariable, catchBody);

            return(Expression.TryCatch(mappingBlock, catchBlock));
        }
Exemple #2
0
        public Expression ToCatchBody(
            Expression exceptionVariable,
            Type returnType,
            IMemberMapperData mapperData)
        {
            var callbackActionType = _callback.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 createExceptionContextCall = Expression.Call(
                exceptionContextCreateMethod,
                contextAccess,
                exceptionVariable);

            var callbackInvocation  = Expression.Invoke(_callback, createExceptionContextCall);
            var returnDefault       = returnType.ToDefaultExpression();
            var configuredCatchBody = Expression.Block(callbackInvocation, returnDefault);

            return(configuredCatchBody);
        }