Example #1
0
        private static Func <TSource, TTarget> BuildMapper()
        {
            Type sourceType = typeof(TSource);
            Type targetType = typeof(TTarget);
            ParameterExpression sourceParameter = Expression.Parameter(sourceType, "source");
            IMappingRunner      runner          = DefaultConfigurationProvider.Current.FindRunner(sourceType);
            ResolutionContext   context         = new ResolutionContext(sourceType, targetType, sourceParameter);
            ResolutionResult    result          = runner.Map(context);

            return(Expression.Lambda <Func <TSource, TTarget> >(Expression.MemberInit(Expression.New(targetType), result.MemberBindings), new ParameterExpression[] { sourceParameter }).Compile());
        }
Example #2
0
        private static Func <TSource, TTarget, TTarget> B2()
        {
            Type sourceType = typeof(TSource);
            Type targetType = typeof(TTarget);
            ParameterExpression sourceParameter = Expression.Parameter(sourceType, "source");

            IConfigurationProvider provider = DefaultConfigurationProvider.Current;
            IMappingRunner         runner   = provider.FindRunner(sourceType);
            ResolutionContext      context  = new ResolutionContext(sourceType, targetType, sourceParameter);

            ResolutionResult result = runner.Map(context);

            Expression initializer = Expression.MemberInit(Expression.New(targetType), result.MemberBindings);
            Func <TSource, TTarget, TTarget> func = Expression.Lambda <Func <TSource, TTarget, TTarget> >(initializer, sourceParameter).Compile();

            return(func);
        }