变量声明未按预期解析

发布于 2024-07-13 06:01:56 字数 1535 浏览 2 评论 0原文

在 C# 中使用 CodeDom,我尝试生成以下 for 循环:

for (int i = 0; i < ds.Tables[0].Rows.Count; i = (i + 1))

除了我的代码生成此循环之外:

for (int i; (i < ds.Tables[0].Rows.Count); i = (i + 1))

请注意,这不会将 i 初始化为零,这不会在 C# 中编译。 (VB 确实接受这一点)。
所以我必须在代码生成后稍后再手动修复它,这很烦人,但我想修复它。 生成整个语句的代码如下:

CodeVariableDeclarationStatement idx = new CodeVariableDeclarationStatement(new CodeTypeReference("System.Int32"), "i", new CodePrimitiveExpression(0));
        CodeIndexerExpression dsIndex = new CodeIndexerExpression(new CodeVariableReferenceExpression("ds.Tables"), new CodeExpression[] { new CodePrimitiveExpression(0) });
        CodeBinaryOperatorExpression comp = new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("i"), CodeBinaryOperatorType.LessThan, new CodePropertyReferenceExpression(dsIndex, "Rows.Count"));
        CodeAssignStatement incr = new CodeAssignStatement(new CodeVariableReferenceExpression("i"), new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("i"), CodeBinaryOperatorType.Add, new CodePrimitiveExpression(1)));
        CodeIterationStatement iterator = new CodeIterationStatement(idx, comp, incr);

根据MSDN 这是初始化值的方法,除非我遗漏了一些微妙的东西。 有人可以帮忙吗?

编辑:代码是正确的。 事实证明,解决方案中的另一个项目引用了许可证已过期的 Redgate-SQL 库,并且它在某种程度上阻止了我的更新正确部署。 感谢您的帮助和时间。

Using CodeDom in C#, I am trying to generate the following for loop:

for (int i = 0; i < ds.Tables[0].Rows.Count; i = (i + 1))

Except that my code is generating this:

for (int i; (i < ds.Tables[0].Rows.Count); i = (i + 1))

Note that this does not initialize i to zero, which does not compile in C#. (VB does accept this).
So I have to go in later after the code is generated and fix it manually, which is mostly just annoying, but I'd like to fix it. The code to generate the entire statement is as follows:

CodeVariableDeclarationStatement idx = new CodeVariableDeclarationStatement(new CodeTypeReference("System.Int32"), "i", new CodePrimitiveExpression(0));
        CodeIndexerExpression dsIndex = new CodeIndexerExpression(new CodeVariableReferenceExpression("ds.Tables"), new CodeExpression[] { new CodePrimitiveExpression(0) });
        CodeBinaryOperatorExpression comp = new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("i"), CodeBinaryOperatorType.LessThan, new CodePropertyReferenceExpression(dsIndex, "Rows.Count"));
        CodeAssignStatement incr = new CodeAssignStatement(new CodeVariableReferenceExpression("i"), new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("i"), CodeBinaryOperatorType.Add, new CodePrimitiveExpression(1)));
        CodeIterationStatement iterator = new CodeIterationStatement(idx, comp, incr);

According to MSDN this is the way to initialize a value, unless there's something subtle I'm missing. Can anybody help?

Edit: The code is correct. It turns out another project in the solution was referencing a Redgate-SQL library that had an expired license, and it was somehow preventing my updates from getting deployed correctly. Thanks for your help and time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

颜漓半夏 2024-07-20 06:01:56

它似乎在我的机器上运行良好。

返回:

for (int i = 0; (i < ds.Tables[0].Rows.Count); i = (i + 1)) {
}

您使用的框架是什么版本?

It seems to work well on my machine.

Returns:

for (int i = 0; (i < ds.Tables[0].Rows.Count); i = (i + 1)) {
}

What version of the framework are you using?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文