CopyTo() public method

public CopyTo ( CodeStatement array, int index ) : void
array CodeStatement
index int
return void
		public void Constructor1_Deny_Unrestricted ()
		{
			CodeStatementCollection coll = new CodeStatementCollection (array);
			coll.CopyTo (array, 0);
			Assert.AreEqual (1, coll.Add (cs), "Add");
			Assert.AreSame (cs, coll[0], "this[int]");
			coll.AddRange (array);
			coll.AddRange (coll);
			Assert.IsTrue (coll.Contains (cs), "Contains");
			Assert.AreEqual (0, coll.IndexOf (cs), "IndexOf");
			coll.Insert (0, cs);
			coll.Remove (cs);
		}
Esempio n. 2
0
        private CodeIterationStatement GenerateForLoop(GroupActivity groupActivity)
        {
            var coreGroupMethodStatement = new CodeStatementCollection();

            // put the current element in the declare variable
            // TODO convert the $Variable in variable like in Xpath
            CodeVariableDeclarationStatement iterationElementSlotDeclaration = new CodeVariableDeclarationStatement("var", groupActivity.IterationElementSlot, new CodeVariableReferenceExpression(groupActivity.Over + "[" + groupActivity.IndexSlot + "]"));
            coreGroupMethodStatement.Add(iterationElementSlotDeclaration);
            // get the core loop code
            coreGroupMethodStatement.AddRange(this.GenerateCoreGroupMethod(groupActivity));
            var array = new CodeStatement[coreGroupMethodStatement.Count];
            coreGroupMethodStatement.CopyTo(array, 0);

            // put it then in the loop
            CodeIterationStatement forLoop = new CodeIterationStatement(new CodeVariableDeclarationStatement(typeof(int), groupActivity.IndexSlot, new CodePrimitiveExpression(0)),
                new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression(groupActivity.IndexSlot), CodeBinaryOperatorType.LessThan, new CodeVariableReferenceExpression(groupActivity.Over + ".Lenght")),
                new CodeAssignStatement(new CodeVariableReferenceExpression(groupActivity.IndexSlot), new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression(groupActivity.IndexSlot), CodeBinaryOperatorType.Add, new CodePrimitiveExpression(1))),
                array);
            return forLoop;
        }
Esempio n. 3
0
 /// <summary>
 /// Codes the STMT col to array.
 /// </summary>
 /// <param name="statmentCollection">The statment collection.</param>
 /// <returns>return CodeStmtColToArray</returns>
 internal static CodeStatement[] CodeStmtColToArray(CodeStatementCollection statmentCollection)
 {
     var tryFinallyStatmanents = new CodeStatement[statmentCollection.Count];
     statmentCollection.CopyTo(tryFinallyStatmanents, 0);
     return tryFinallyStatmanents;
 }
        public CodeStatement[] ToCodeDomArray()
        {
            CodeStatementCollection col = new CodeStatementCollection();
            ToCodeDom(col);

            CodeStatement[] sts = new CodeStatement[col.Count];
            col.CopyTo(sts,0);

            return sts;
        }
Esempio n. 5
0
        private CodeMemberMethod BuildNativeWriteObjectMethod(string type, string typeName, CodeNamespace codeNs)
        {
            // Create WriteObject method
            CodeMemberMethod codeMemberMethod = new CodeMemberMethod();
            codeMemberMethod.Attributes = MemberAttributes.Public | MemberAttributes.Override;
            codeMemberMethod.ReturnType = new CodeTypeReference(typeof(void));
            codeMemberMethod.Name = "WriteObject";
            codeMemberMethod.Parameters.Add(new CodeParameterDeclarationExpression("XmlWriter", "writer"));
            codeMemberMethod.Parameters.Add(new CodeParameterDeclarationExpression(typeof(object), "graph"));

            // Add type declaration
            CodeVariableDeclarationStatement declaration = new CodeVariableDeclarationStatement(type, typeName);
            declaration.InitExpression = new CodeCastExpression(type, new CodeVariableReferenceExpression("graph"));
            codeMemberMethod.Statements.Add(declaration);

            // Create temporary member field used to generate write string code
            CodeMemberField memberField = new CodeMemberField(type, typeName);

            // Create conditional write statement
            CodeStatementCollection writeStatements = new CodeStatementCollection();
            if (memberField.Type.ArrayElementType != null && CodeGenUtils.IsNativeClrType(memberField.Type.ArrayElementType.BaseType))
            {
                writeStatements.AddRange(BuildWriteArrayStatements(null, memberField));
            }
            // Else if this is a native type
            else if (Type.GetType(memberField.Type.BaseType) != null)
            {
                writeStatements.Add(BuildWriteStringStatement(null, memberField));
            }
            else
                throw new ArgumentException("Invalid native field type for schema item: " + memberField.Name);

            // Add WriteEndElement statement
            writeStatements.Add(new CodeMethodInvokeExpression(new CodeVariableReferenceExpression("writer"),
                "WriteEndElement", new CodeExpression[] { }));

            // Convert statement collection to array
            CodeStatement[] writeStatementArray = new CodeStatement[writeStatements.Count];
            writeStatements.CopyTo(writeStatementArray, 0);

            // Build WriteParentElement condition paramters
            CodeVariableReferenceExpression writerParam = new CodeVariableReferenceExpression("writer");
            CodePrimitiveExpression nillableParam = new CodePrimitiveExpression(false);
            CodePrimitiveExpression requiredParam = new CodePrimitiveExpression(true);
            CodeVariableReferenceExpression objectParam = new CodeVariableReferenceExpression("graph");

            // Build WriteParentElement condition statement
            CodeConditionStatement condition = new CodeConditionStatement(new CodeMethodInvokeExpression(null,
                "WriteParentElement",
                new CodeExpression[] { writerParam, nillableParam, requiredParam, objectParam }),
                // True condition code. Build read statements based on contract member type
                writeStatementArray
            );

            // Add condition
            codeMemberMethod.Statements.Add(condition);

            return codeMemberMethod;
        }
Esempio n. 6
0
        private CodeMemberMethod BuildNativeReadObjectMethod(string type, string typeName)
        {
            // Create ReadObject method
            CodeMemberMethod codeMemberMethod = new CodeMemberMethod();
            codeMemberMethod.Attributes = MemberAttributes.Public | MemberAttributes.Override;
            codeMemberMethod.ReturnType = new CodeTypeReference(typeof(object));
            codeMemberMethod.Name = "ReadObject";
            codeMemberMethod.Parameters.Add(new CodeParameterDeclarationExpression("XmlReader", "reader"));

            // Create temporary variable
            CodeVariableDeclarationStatement varDecl = new CodeVariableDeclarationStatement(type, typeName);
            if (varDecl.Type.ArrayElementType == null)
            {
                if (type == "System.String")
                    varDecl.InitExpression = new CodePrimitiveExpression(null);
            }
            else
                varDecl.InitExpression = new CodeArrayCreateExpression(type, new CodeExpression[] { });
            codeMemberMethod.Statements.Add(varDecl);

            // Create temporary member field used to generate read string code
            CodeMemberField memberField = new CodeMemberField(type, typeName);

            // Create conditional read statement
            CodeStatementCollection readStatements = new CodeStatementCollection();
            if (memberField.Type.ArrayElementType != null && CodeGenUtils.IsNativeClrType(memberField.Type.ArrayElementType.BaseType))
            {
                readStatements.AddRange(BuildReadStringArrayStatements(MemberType.Field, null, memberField));
            }
            // Else if this is a native type
            else if (Type.GetType(memberField.Type.BaseType) != null)
            {
                readStatements.Add(BuildReadStringStatement(MemberType.Field, null, memberField));
            }
            else
                throw new ArgumentException("Invalid native field type for schema item: " + memberField.Name);

            // Add ReadEndElement to condition statements
            readStatements.Add(new CodeMethodInvokeExpression(new CodeVariableReferenceExpression("reader"),
                "ReadEndElement", new CodeExpression[] { }));

            // Add native type return statement
            readStatements.Add(new CodeMethodReturnStatement(new CodeArgumentReferenceExpression(typeName)));

            // Convert statement collection to array
            CodeStatement[] readStatementArray = new CodeStatement[readStatements.Count];
            readStatements.CopyTo(readStatementArray, 0);

            // Build IsParentStartElement if statement
            CodeVariableReferenceExpression readerParam = new CodeVariableReferenceExpression("reader");
            CodePrimitiveExpression nillableParam = new CodePrimitiveExpression(false);
            CodePrimitiveExpression requiredParam = new CodePrimitiveExpression(true);
            CodeConditionStatement condition = new CodeConditionStatement(new CodeMethodInvokeExpression(null,
                "IsParentStartElement",
                new CodeExpression[] { readerParam, nillableParam, requiredParam }),
                readStatementArray);

            // Add condition
            codeMemberMethod.Statements.Add(condition);

            // Add null return statement
            codeMemberMethod.Statements.Add(new CodeMethodReturnStatement(new CodePrimitiveExpression(null)));

            return codeMemberMethod;
        }
Esempio n. 7
0
        private CodeStatementCollection BuildMtomReadStringStatements(MemberType memberType, string classRefName, CodeMemberField memberField)
        {
            CodeStatementCollection statements = new CodeStatementCollection();
            CodeStatementCollection trueStatements = new CodeStatementCollection();
            CodeStatementCollection falseStatements = new CodeStatementCollection();

            // string contentID
            trueStatements.Add(
                new CodeVariableDeclarationStatement(
                    typeof(string),
                    "contentID"
                )
            );
            // contentID = reader.Value;
            trueStatements.Add(
                new CodeAssignStatement(
                    new CodeVariableReferenceExpression("contentID"),
                    new CodeFieldReferenceExpression(
                        new CodeVariableReferenceExpression("reader"),
                        "Value"
                    )
                )
            );
            // reader.MoveToElement();
            trueStatements.Add(
                new CodeMethodInvokeExpression(
                    new CodeVariableReferenceExpression("reader"),
                    "MoveToElement"
                )
            );
            // reader.ReadStartElement("Include", "http://www.w3.org/2004/08/xop/include");
            trueStatements.Add(
                new CodeMethodInvokeExpression(
                    new CodeVariableReferenceExpression("reader"),
                    "ReadStartElement",
                    new CodeExpression[] { 
                            new CodePrimitiveExpression("Include"),
                            new CodePrimitiveExpression("http://www.w3.org/2004/08/xop/include")
                        }
                )
            );
            // reader.ReadEndElement();
            trueStatements.Add(
                new CodeMethodInvokeExpression(
                    new CodeVariableReferenceExpression("reader"),
                    "ReadEndElement"
                )
            );
            // ObjectName.FieldName = GetBodyPartContent(contentID)
            trueStatements.Add(
                new CodeAssignStatement(
                    new CodeFieldReferenceExpression(
                        new CodeVariableReferenceExpression(classRefName),
                        memberField.Name
                    ),
                    new CodeMethodInvokeExpression(
                        null,
                        "GetBodyPartContent",
                        new CodeExpression[] {
                            new CodeVariableReferenceExpression("contentID"),
                            new CodeVariableReferenceExpression("BodyParts")
                        }
                    )
                )
            );

            falseStatements.Add(
                new CodeAssignStatement(
                    new CodeFieldReferenceExpression(
                        new CodeVariableReferenceExpression(classRefName),
                        memberField.Name
                    ),
                    new CodeMethodInvokeExpression(
                        new CodeTypeReferenceExpression("Convert"),
                        "FromBase64String",
                        new CodeExpression[] {
                            new CodeMethodInvokeExpression( 
                                new CodeVariableReferenceExpression("reader"), 
                                "ReadString")
                        }
                    )
                )
            );

            falseStatements.Add(
                new CodeMethodInvokeExpression(
                    new CodeVariableReferenceExpression("reader"),
                    "ReadEndElement"
                )
            );

            CodeStatement[] trues = new CodeStatement[trueStatements.Count];
            CodeStatement[] falses = new CodeStatement[falseStatements.Count];

            trueStatements.CopyTo(trues, 0);
            falseStatements.CopyTo(falses, 0);

            // if( IsAttribute(reader, "href") )
            statements.Add(
                new CodeConditionStatement( 
                    new CodeMethodInvokeExpression(
                        null,
                        "IsAttribute",
                        new CodeExpression[] {
                            new CodeVariableReferenceExpression("reader"),
                            new CodePrimitiveExpression("href")
                        }
                    ),
                    trues,
                    falses
                )
            );
            return statements;
        }
Esempio n. 8
0
        private CodeMemberMethod GenerateTypeArrayJSONArray()
        {
            var methodDecl = new CodeMemberMethod
            {
                Name = methodName,
                Attributes = MemberAttributes.Public | MemberAttributes.Final | MemberAttributes.Static
            };
            methodDecl.Parameters.Add(new CodeParameterDeclarationExpression(new CodeTypeReference(type.FullName + "[]"), "objs"));
            methodDecl.Parameters.Add(new CodeParameterDeclarationExpression(new CodeTypeReference("Org.Json.JSONArray"), "jsonArray"));

            var innerStatements = new CodeStatementCollection();
            FillJSONArrayMembers(innerStatements);

            var innerStatementsArray = new CodeStatement[innerStatements.Count];
            innerStatements.CopyTo(innerStatementsArray, 0);

            methodDecl.Statements.Add(
                new CodeIterationStatement(new CodeVariableDeclarationStatement(typeof(int), "i", new CodeSnippetExpression("0")),
                                           new CodeSnippetExpression("i < objs.Length"), new CodeSnippetStatement("i++"),
                                           innerStatementsArray));

            return methodDecl;
        }
        static CodeStatement[] GetRequiredLocationsConditionStatements(IList<string> requiredLocations)
        {
            CodeStatementCollection statementCollection = new CodeStatementCollection();
            foreach (string locationName in requiredLocations)
            {
                CodeMethodInvokeExpression invokeValidateExpression = new CodeMethodInvokeExpression(
                new CodeMethodReferenceExpression(new CodeVariableReferenceExpression("returnLocations"), "Add"),
                new CodePrimitiveExpression(locationName));
                statementCollection.Add(invokeValidateExpression);
            }

            CodeStatement[] returnStatements = new CodeStatement[statementCollection.Count];
            statementCollection.CopyTo(returnStatements, 0);

            return returnStatements;
        }
        CodeStatement[] GenerateStatementInvocation(CompiledExpressionDescriptor descriptor, bool withLocationReferences, Dictionary<string, int> cacheIndicies)
        {
            string indexString = descriptor.Id.ToString(CultureInfo.InvariantCulture);
            string dataContextVariableName = "valDataContext" + indexString;

            CodeVariableDeclarationStatement dataContextVariable = new CodeVariableDeclarationStatement(
                     new CodeTypeReference(descriptor.TypeName), dataContextVariableName);

            CodeStatementCollection compiledDataContextStatements = new CodeStatementCollection();

            GenerateGetDataContextVariable(descriptor, dataContextVariable, compiledDataContextStatements, withLocationReferences, cacheIndicies);
            compiledDataContextStatements.Add(dataContextVariable);
                        
            CodeMethodInvokeExpression expressionInvoke = new CodeMethodInvokeExpression(
                new CodeMethodReferenceExpression(
                    new CodeVariableReferenceExpression(dataContextVariableName), descriptor.StatementMethodName));
            
            CodeMethodReturnStatement returnStatement = new CodeMethodReturnStatement(new CodePrimitiveExpression(null));

            compiledDataContextStatements.Add(expressionInvoke);
            compiledDataContextStatements.Add(returnStatement);

            CodeStatement[] returnStatements = new CodeStatement[compiledDataContextStatements.Count];
            compiledDataContextStatements.CopyTo(returnStatements, 0);

            return returnStatements;
        }
        CodeStatement[] GenerateReferenceExpressionInvocation(CompiledExpressionDescriptor descriptor, bool withLocationReferences, Dictionary<string, int> cacheIndicies)
        {
            string indexString = descriptor.Id.ToString(CultureInfo.InvariantCulture);
            string dataContextVariableName = "refDataContext" + indexString;

            CodeVariableDeclarationStatement dataContextVariable = new CodeVariableDeclarationStatement(
                     new CodeTypeReference(descriptor.TypeName), dataContextVariableName);

            CodeStatementCollection compiledDataContextStatements = new CodeStatementCollection();

            GenerateGetDataContextVariable(descriptor, dataContextVariable, compiledDataContextStatements, withLocationReferences, cacheIndicies);
            compiledDataContextStatements.Add(dataContextVariable);

            CodeExpression getExpression = null;
            CodeExpression setExpression = null;

            if (this.IsVB)
            {
                getExpression = new CodeDelegateCreateExpression(
                        new CodeTypeReference(descriptor.TypeName),
                        new CodeVariableReferenceExpression(dataContextVariableName),
                        descriptor.GetMethodName);
                setExpression = new CodeDelegateCreateExpression(
                        new CodeTypeReference(descriptor.TypeName),
                        new CodeVariableReferenceExpression(dataContextVariableName),
                        descriptor.SetMethodName);
            }
            else
            {
                getExpression = new CodeMethodReferenceExpression(new CodeVariableReferenceExpression(dataContextVariableName), descriptor.GetMethodName);
                setExpression = new CodeMethodReferenceExpression(new CodeVariableReferenceExpression(dataContextVariableName), descriptor.SetMethodName);
            }

            CodeMethodReferenceExpression getLocationMethod = new CodeMethodReferenceExpression(
                new CodeVariableReferenceExpression(dataContextVariableName),
                "GetLocation",
                new CodeTypeReference[] { new CodeTypeReference(descriptor.ResultType) });

            CodeExpression[] getLocationParameters = null;
            if (withLocationReferences)
            {
                getLocationParameters = new CodeExpression[] { 
                    getExpression,  
                    setExpression,
                    new CodeVariableReferenceExpression("expressionId"),
                    new CodeFieldReferenceExpression(
                        new CodeThisReferenceExpression(),
                        rootActivityFieldName),
                    new CodeVariableReferenceExpression("activityContext") };
            }
            else
            {
                getLocationParameters = new CodeExpression[] { 
                    getExpression,  
                    setExpression };
            }

            CodeMethodInvokeExpression getLocationExpression = new CodeMethodInvokeExpression(
                getLocationMethod,
                getLocationParameters);


            CodeMethodReturnStatement returnStatement = new CodeMethodReturnStatement(getLocationExpression);

            compiledDataContextStatements.Add(returnStatement);
            
            CodeStatement[] returnStatements = new CodeStatement[compiledDataContextStatements.Count];
            compiledDataContextStatements.CopyTo(returnStatements, 0);

            return returnStatements;
        }
Esempio n. 12
0
        private CodeMemberMethod GenerateTypeArrayJSONArray()
        {
            var methodDecl = new CodeMemberMethod
            {
                Name = methodName + "Array",
                Attributes = MemberAttributes.Public | MemberAttributes.Final | MemberAttributes.Static
            };
            methodDecl.ReturnType = new CodeTypeReference(type.FullName + "[]");
            methodDecl.Parameters.Add(new CodeParameterDeclarationExpression(new CodeTypeReference("Org.Json.JSONArray"), "jsonArray"));

            var innerStatements = new CodeStatementCollection();
            FillJSONArrayMembers(innerStatements);

            var innerStatementsArray = new CodeStatement[innerStatements.Count];
            innerStatements.CopyTo(innerStatementsArray, 0);

            methodDecl.Statements.Add(new CodeVariableDeclarationStatement(new CodeTypeReference("System.Collections.Generic.List<" + type.FullName + ">"), "result", new CodeObjectCreateExpression("System.Collections.Generic.List<" + type.FullName + ">")));

            methodDecl.Statements.Add(new CodeVariableDeclarationStatement(typeof(int), "length",
                                                                          new CodeMethodInvokeExpression(new CodeVariableReferenceExpression("jsonArray"), "Length")));

            methodDecl.Statements.Add(
                new CodeIterationStatement(new CodeVariableDeclarationStatement(typeof(int), "i", new CodeSnippetExpression("0")),
                                           new CodeSnippetExpression("i < length"), new CodeSnippetStatement("i++"),
                                           innerStatementsArray));

            methodDecl.Statements.Add(new CodeMethodReturnStatement( new CodeMethodInvokeExpression( new CodeVariableReferenceExpression("result"), "ToArray")));

            return methodDecl;
        }
        private IEnumerable<CodeStatement> GetPathTraversalStatements()
        {
            string exp = "";
            IList<AggregateExpressionPathItem> pathItems
                = ExpressionParser.BuildAggregatePathItem(m_contexts[0].Descriptor, m_contexts[0].Member);

            foreach (AggregateExpressionPathItem pathItem in pathItems)
            {
                if (pathItem.IsCollection)
                {
                    exp += string.Format("foreach({0} {1} in {2}.{3})",
                        TypeHelper.GetTypeDefinition(pathItem.Type),
                        pathItem.Object,
                        pathItem.Target,
                        pathItem.Expression);
                }
            }

            exp += Environment.NewLine + "\t\t\t\t{" + Environment.NewLine;
            foreach (AggregateFunctionContext context in m_contexts)
            {
                IEnumerable<string> itemExpressions = context.Generator.GetIterationStatements(context, context.PathItems);
                foreach (string itemExpression in itemExpressions)
                {
                    exp += "\t\t\t\t\t";
                    exp += itemExpression;
                    if(!exp.EndsWith(";"))
                        exp += ";";
                    exp += Environment.NewLine; // todo: smarter way
                }
            }
            exp += "\t\t\t\t}";

            CodeConditionStatement ifStatement = new CodeConditionStatement(
                new CodeSnippetExpression(pathItems[0].Target + "." + pathItems[0].Expression + " != null"),
                new CodeStatement[] { new CodeExpressionStatement(new CodeSnippetExpression(exp)) },
                new CodeStatement[0]);

            CodeStatementCollection statements = new CodeStatementCollection();
            statements.Add(ifStatement);

            CodeStatement[] arr = new CodeStatement[statements.Count];
            statements.CopyTo(arr, 0);
            return arr;
        }