Beispiel #1
0
        protected Expression VisitNonTranslatedMemberInit(MemberInitExpression init)
        {
            if (init.NewExpression.Arguments.Count > 0)
            {
                throw new NotSupportedException("Only parameterless constructor is supported.");
            }

            var memberAssignments = new List <KeyValuePair <MemberInfo, Expression> >();
            var memberQueryTypes  = new Dictionary <MemberInfo, SimpleType>();

            //Currently only considering member assignment bindings
            foreach (var binding in init.Bindings)
            {
                switch (binding.BindingType)
                {
                case MemberBindingType.Assignment:
                    var result = VisitWithNewVisitor((binding as MemberAssignment).Expression, AnalysisContext, false);
                    memberAssignments.Add(new KeyValuePair <MemberInfo, Expression>(binding.Member, result.TranslatedExpression));
                    memberQueryTypes.Add(binding.Member, result.QueryableType);
                    break;

                default:
                    throw new NotSupportedException("Only member assignment is supported.");
                }
            }

            var typeArgs = memberAssignments.Select(kvp => kvp.Value.Type).ToList();
            var newType  = AnalysisContext.TypeTranslationUtil.GetAnonymousType(typeArgs);
            var nex      = Expression.New(newType.GetConstructor(Type.EmptyTypes));

            var nonTranslatableType = new NonTranslatableType(init.NewExpression.Type, newType,
                                                              init.NewExpression.Constructor, memberQueryTypes);

            var ctr = 0;

            memberAssignments.ForEach(kvp => nonTranslatableType.MemberMap.Add(kvp.Key, newType.GetProperty("Field" + (ctr++))));
            var bindings = memberAssignments.Select(kvp => Expression.Bind(nonTranslatableType.MemberMap[kvp.Key], kvp.Value) as MemberBinding).ToList();

            AnalysisContext.QueryableType = nonTranslatableType;
            return(Expression.MemberInit(nex, bindings));
        }
        protected Expression VisitNonTranslatedMemberInit(MemberInitExpression init)
        {
            if (init.NewExpression.Arguments.Count > 0)
                throw new NotSupportedException("Only parameterless constructor is supported.");

            var memberAssignments = new List<KeyValuePair<MemberInfo, Expression>>();
            var memberQueryTypes = new Dictionary<MemberInfo, SimpleType>();            
            //Currently only considering member assignment bindings
            foreach(var binding in init.Bindings)
            {
                switch(binding.BindingType)
                {
                    case MemberBindingType.Assignment:
                        var result = VisitWithNewVisitor((binding as MemberAssignment).Expression, AnalysisContext, false);
                        memberAssignments.Add(new KeyValuePair<MemberInfo, Expression>(binding.Member, result.TranslatedExpression));
                        memberQueryTypes.Add(binding.Member, result.QueryableType);
                        break;
                    default:
                        throw new NotSupportedException("Only member assignment is supported.");
                }
            }

            var typeArgs = memberAssignments.Select(kvp => kvp.Value.Type).ToList();
            var newType = AnalysisContext.TypeTranslationUtil.GetAnonymousType(typeArgs);
            var nex = Expression.New(newType.GetConstructor(Type.EmptyTypes));

            var nonTranslatableType = new NonTranslatableType(init.NewExpression.Type, newType, 
                init.NewExpression.Constructor, memberQueryTypes);

            var ctr = 0;
            memberAssignments.ForEach(kvp => nonTranslatableType.MemberMap.Add(kvp.Key, newType.GetProperty("Field" + (ctr++))));
            var bindings = memberAssignments.Select(kvp => Expression.Bind(nonTranslatableType.MemberMap[kvp.Key], kvp.Value) as MemberBinding).ToList();

            AnalysisContext.QueryableType = nonTranslatableType;
            return Expression.MemberInit(nex, bindings);
        }