本文整理汇总了C#中System.CodeDom.CodeIterationStatement类的典型用法代码示例。如果您正苦于以下问题:C# CodeIterationStatement类的具体用法?C# CodeIterationStatement怎么用?C# CodeIterationStatement使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
CodeIterationStatement类属于System.CodeDom命名空间,在下文中一共展示了CodeIterationStatement类的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CodeVariableDeclarationStatement
// Declares and initializes an integer variable named testInt.
CodeVariableDeclarationStatement testInt = new CodeVariableDeclarationStatement(typeof(int), "testInt", new CodePrimitiveExpression(0) );
// Creates a for loop that sets testInt to 0 and continues incrementing testInt by 1 each loop until testInt is not less than 10.
CodeIterationStatement forLoop = new CodeIterationStatement(
// initStatement parameter for pre-loop initialization.
new CodeAssignStatement( new CodeVariableReferenceExpression("testInt"), new CodePrimitiveExpression(1) ),
// testExpression parameter to test for continuation condition.
new CodeBinaryOperatorExpression( new CodeVariableReferenceExpression("testInt"),
CodeBinaryOperatorType.LessThan, new CodePrimitiveExpression(10) ),
// incrementStatement parameter indicates statement to execute after each iteration.
new CodeAssignStatement( new CodeVariableReferenceExpression("testInt"), new CodeBinaryOperatorExpression(
new CodeVariableReferenceExpression("testInt"), CodeBinaryOperatorType.Add, new CodePrimitiveExpression(1) )),
// statements parameter contains the statements to execute during each interation of the loop.
// Each loop iteration the value of the integer is output using the Console.WriteLine method.
new CodeStatement[] { new CodeExpressionStatement( new CodeMethodInvokeExpression( new CodeMethodReferenceExpression(
new CodeTypeReferenceExpression("Console"), "WriteLine" ), new CodeMethodInvokeExpression(
new CodeVariableReferenceExpression("testInt"), "ToString" ) ) ) } );
// A C# code generator produces the following source code for the preceeding example code:
// int testInt = 0;
// for (testInt = 1; (testInt < 10); testInt = (testInt + 1)) {
// Console.WriteLine(testInt.ToString());