private static LightExpression.Expression <Func <Source, Dest, ResolutionContext, Dest> > CreateLightExpression() { var srcParam = L.Parameter(typeof(Source), "source"); var destParam = L.Parameter(typeof(Dest), "dest"); var exceptionVar = L.Parameter(typeof(Exception), "ex"); var typeMapDestVar = L.Parameter(typeof(Dest), "d"); var resolvedValueVar = L.Parameter(typeof(int), "val"); var expression = L.Lambda <Func <Source, Dest, ResolutionContext, Dest> >( L.Block( L.Condition( L.Equal(srcParam, L.Constant(null)), L.Default(typeof(Dest)), L.Block(typeof(Dest), new[] { typeMapDestVar }, L.Assign( typeMapDestVar, L.Coalesce(destParam, L.New(typeof(Dest).GetTypeInfo().DeclaredConstructors.First()))), L.TryCatch( /* Assign src.Value */ L.Block(new[] { resolvedValueVar }, L.Block( L.Assign(resolvedValueVar, L.Condition(L.Or(L.Equal(srcParam, L.Constant(null)), L.Constant(false)), L.Default(typeof(int)), L.Property(srcParam, "Value")) ), L.Assign(L.Property(typeMapDestVar, "Value"), resolvedValueVar) ) ), L.Catch(exceptionVar, L.Throw( L.New(typeof(AutoMapperException).GetTypeInfo().DeclaredConstructors.First(), L.Constant("Error mapping types."), exceptionVar), typeof(int))) // should skip this, cause does no make sense after the throw ), typeMapDestVar)) ), srcParam, destParam, L.Parameter(typeof(ResolutionContext), "_") ); return(expression); }