Ejemplo n.º 1
0
        public static Func <TFrom, TTo> GetConverter_CompiledFast_LightExpression <TFrom, TTo>()
        {
            var fromParam = L.Parameter(typeof(TFrom));
            var expr      = L.Lambda <Func <TFrom, TTo> >(L.Convert(fromParam, typeof(TTo)), fromParam);

            return(LightExpression.ExpressionCompiler.CompileFast(expr, true));
        }
        public object CreateExpressionInfo_and_FastCompile()
        {
            //Expression<Func<Action<string>>> expr = () => a => s.SetValue(a);

            var aParam = LE.Parameter(typeof(string), "a");
            var expr   = LE.Lambda(LE.Lambda(LE.Call(LE.Constant(_s), _setValueMethod, aParam), aParam));

            return(LEC.TryCompile <Func <Action <string> > >(expr));
        }
        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);
        }