如何用CodeDom实现While循环

很多人都说用CodeDom无法实现While循环,可MSDN上的说明明明说可以.很奇怪.自己试了一下,发现其实是可以实现的,只是形式上象For循环而已.

method.Statements.Add(new CodeIterationStatement(new CodeExpressionStatement(new CodeSnippetExpression("")), new CodeMethodInvokeExpression(new CodeVariableReferenceExpression("reader"), "Read", new CodeExpression[] { }), new CodeExpressionStatement(new CodeSnippetExpression("")), new CodeStatement[] { new CodeVariableDeclarationStatement(helper.GetClassName(ATableSection.TableName), "obj", new CodeObjectCreateExpression(helper.GetClassName(ATableSection.TableName), new CodeExpression[] { })), new CodeExpressionStatement(new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(helper.GetClassName(ATableSection.TableName)), "ReadValue", new CodeExpression[] { new CodeVariableReferenceExpression("reader"), new CodeVariableReferenceExpression("obj") })), new CodeExpressionStatement(new CodeMethodInvokeExpression(new CodeVariableReferenceExpression("list"), "Add", new CodeVariableReferenceExpression("obj"))) }));

以上代码插入了一个初始语和循环结束语句均为空的For循环,其实质上就是一个While特环,生成的代码如下:

 

for (; reader.Read(); ) {
Activiey obj = new Activiey();
Activiey.ReadValue(reader, obj);
list.Add(obj);
}

 

CodeDom本身是支持While循环的,只不过特定的Provider并没有输出成While的格式而已.这应当不是CodeDom的问题.

你可能感兴趣的:(while)