官术网_书友最值得收藏!

How to do it...

  1. In Solution Explorer, double-click on the Resources.resx file in CSharpAnalyzers project to open the resource file in the resource editor.
  2. Replace the existing resource strings for AnalyzerDescription, AnalyzerMessageFormat and AnalyzerTitle with new strings.
  1. Replace the Initialize method implementation with the following:
public override void Initialize(AnalysisContext context)
{
context.RegisterSyntaxTreeAction(syntaxTreeContext =>
{
// Iterate through all statements in the tree.
var root = syntaxTreeContext.Tree.GetRoot(syntaxTreeContext.CancellationToken);
foreach (var statement in root.DescendantNodes().OfType<StatementSyntax>())
{
// Skip analyzing block statements.
if (statement is BlockSyntax)
{
continue;
}

// Report issue for all statements that are nested within a statement,
// but not a block statement.
if (statement.Parent is StatementSyntax && !(statement.Parent is BlockSyntax))
{
var diagnostic = Diagnostic.Create(Rule, statement.GetFirstToken().GetLocation());
syntaxTreeContext.ReportDiagnostic(diagnostic);
}
}
});
}
  1. Click on Ctrl + F5 to start a new Visual Studio instance with the analyzer enabled.
  2. In the new Visual Studio instance, create a new C# class library with the following code:
namespace ClassLibrary
{
public class Class1
{
void Method(bool flag, int value)
{
while (flag)
if (value > 0)
System.Console.WriteLine(value);
}
}
}
  1. Verify the analyzer diagnostic is neither reported for the method block for Method nor the while statement, but is reported for the if statement and System.Console.WriteLine invocation statement:
  1. Now, add curly braces around the System.Console.WriteLine invocation statement and verify the only single warning is now reported for the if statement:
主站蜘蛛池模板: 西乌珠穆沁旗| 新巴尔虎左旗| 古交市| 股票| 山阳县| 梅河口市| 盱眙县| 安图县| 交城县| 赤峰市| 尖扎县| 阿拉尔市| 鄂尔多斯市| 新巴尔虎右旗| 宁国市| 镇江市| 普陀区| 米易县| 嘉兴市| 西林县| 炉霍县| 白玉县| 冷水江市| 喀什市| 万安县| 综艺| 新干县| 额敏县| 海城市| 石棉县| 湖州市| 赣榆县| 会宁县| 邓州市| 中卫市| 古田县| 岗巴县| 江陵县| 苗栗县| 尤溪县| 新密市|