简体   繁体   English

在Visual Studio中自动检查嵌套语句

[英]Automated check for nested statements in Visual Studio

As part of my apprenticement, I have to find a way to (automatically) search for statements nested more than 4 levels down. 作为我的学徒计划的一部分,我必须找到一种方法(自动)来搜索嵌套在4层以下的语句。

I use Visual Studio (2012) and C# programming language. 我使用Visual Studio(2012)和C#编程语言。

An example of a (faulty) nested method. (错误的)嵌套方法的示例。

foreach (int i in items) //1
{
    Console.WriteLine(i);
    foreach (int a in items2) //2
    {
        Console.Write(a);
        foreach (int b in items3) //3
        {
            Console.Write(b);
            foreach (int c in items4) //4
            {
                Console.Write(c);
                foreach (int d in items5) //5
                {
                    // Here an error/warning should be shown because it is nested too deep
                    Console.Write(d);
                    foreach (int e in items5)
                    {
                        Console.Write(e);
                    }

                }
            }
        }
    }
}

I have tried using ReSharper(7.1) but this does not (to my knowledge) offer this feature. 我尝试使用ReSharper(7.1),但是(据我所知)它不提供此功能。

How can I achieve this? 我该如何实现?

Resharper can check this, but you will probably need to build a custom Plugin for it, as could a DxCore plugin (CodeRush) from Developer Express. Resharper可以检查此问题,但您可能需要为其构建自定义插件 ,Developer Express的DxCore插件 (CodeRush)也会这样做 StyleCop or FxCop (Code Analysis) can also check something like this in an automated way using a custom written rule. StyleCopFxCop(代码分析)还可以使用自定义书面规则以自动化方式检查类似内容。

There's also a CTP out for Roslyn , the new compiler and parser framework for Visual Studio that may ship with the next version of the product. Roslyn也有CTP ,这是Visual Studio的新编译器和解析器框架,该产品可能与产品的下一版本一起提供。 Roslyn also allows you to inspect the code and add warnings or provide fixes. Roslyn还允许您检查代码并添加警告或提供修复程序。

Try any of these and if you run into issues, either update this post with more details on what you've tried and what works or doesn't or post a new question. 尝试任何一种方法,如果遇到问题,请更新此帖子,以获取有关您尝试过的内容,有效方法或无效方法的更多详细信息,或者发布新问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM