Пример #1
0
        public static Expression <Func <TSource, TDestination> > Add <TSource, TDestination>(this Expression <Func <TSource, TDestination> > first, params Expression <Func <TSource, TDestination> >[] selectors)
        {
            var zeroth = ((MemberInitExpression)first.Body);
            var param  = first.Parameters[0];
            List <MemberBinding> bindings = new List <MemberBinding>(zeroth.Bindings.OfType <MemberAssignment>());

            for (int i = 0; i < selectors.Length; i++)
            {
                var memberInit = (MemberInitExpression)selectors[i].Body;
                var replace    = new ParameterRebinder(selectors[i].Parameters[0], param);
                foreach (var binding in memberInit.Bindings.OfType <MemberAssignment>())
                {
                    bindings.Add(Expression.Bind(binding.Member, replace.VisitAndConvert(binding.Expression, "Combine")));
                }
            }

            return(Expression.Lambda <Func <TSource, TDestination> >(Expression.MemberInit(zeroth.NewExpression, bindings), param));
        }
Пример #2
0
        public static Expression <Func <TSource, TDestination> > Ignore <TSource, TDestination>(this Expression <Func <TSource, TDestination> > first, params Expression <Func <TSource, TDestination> >[] selectors)
        {
            var zeroth = ((MemberInitExpression)first.Body);
            var param  = first.Parameters[0];
            List <MemberBinding> bindings = new List <MemberBinding>(zeroth.Bindings.OfType <MemberAssignment>());

            for (int i = 0; i < selectors.Length; i++)
            {
                var memberInit = (MemberInitExpression)selectors[i].Body;
                var replace    = new ParameterRebinder(selectors[i].Parameters[0], param);
                foreach (var binding in memberInit.Bindings.OfType <MemberAssignment>())
                {
                    var item = bindings.Where(x => x.BindingType == binding.BindingType && x.Member == binding.Member).FirstOrDefault();
                    bindings.Remove(item);
                }
            }

            return(Expression.Lambda <Func <TSource, TDestination> >(Expression.MemberInit(zeroth.NewExpression, bindings), param));
        }