コード例 #1
0
        public override IEnumerable <CodeStatement> GetInitializationStatements(AggregateFunctionContext context)
        {
            MemberMappingDescriptor member = context.Member;
            Type resultType;
            Type instanceType;
            bool isSimpleMapping = false;

            if (member.IsArray || member.IsList)
            {
                instanceType = member.IsArray ?
                               member.AggregateMappingDescription.TargetType.GetElementType() :
                               member.AggregateMappingDescription.TargetType.GetGenericArguments()[0];
                resultType = typeof(List <>).MakeGenericType(instanceType);

                if (!instanceType.IsPrimitive && instanceType != typeof(string))
                {
                    m_expression = string.Format("{{0}}.Add({0}.AssembleFrom({{1}}));", GetAssemblerName(context));
                }
                else
                {
                    m_expression    = "{0}.Add({1});";
                    isSimpleMapping = true;
                }
            }
            else
            {
                string msg = ErrorBuilder.CantAggregateOverNoncollectionError(member, "collect");
                throw new OtisException(msg);                 //todo: test
            }

            m_isArray = member.IsArray;
            List <CodeStatement> statements = new List <CodeStatement>();

            CodeExpression[] parameters = new CodeExpression[0];
            CodeStatement    st         = new CodeVariableDeclarationStatement(resultType,
                                                                               context.FunctionObjectName,
                                                                               new CodeObjectCreateExpression(resultType, parameters));

            statements.Add(st);

            if (!isSimpleMapping)
            {
                st = new CodeVariableDeclarationStatement(
                    string.Format("IAssembler<{0}, {1}>", TypeHelper.GetTypeDefinition(instanceType), TypeHelper.GetTypeDefinition(context.SourceItemType)),
                    GetAssemblerName(context),
                    new CodeSnippetExpression("this"));
                statements.Add(st);
            }

            return(statements);
        }