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

  • Roslyn Cookbook
  • Manish Vasani
  • 226字
  • 2021-07-15 17:07:34

How to do it...

  1. In Solution Explorer, double click on 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 code from CSharpAnalyzers/CSharpAnalyzers/CSharpAnalyzers/DiagnosticAnalyzer.cs/ method named Initialize.

 

  1. Add a private class CompilationAnalyzer from CSharpAnalyzers/CSharpAnalyzers/CSharpAnalyzers/DiagnosticAnalyzer.cs/ type named CompilationAnalyzer in your analyzer to perform the core method body analysis for a given method.
  2. Click on Ctrl + F5 to start a new Visual Studio instance with the analyzer enabled.
  3. In the new Visual Studio instance, enable full solution analysis for C# projects by following the steps here: https://msdn.microsoft.com/en-us/library/mt709421.aspx
  1. In the new Visual Studio instance, create a new C# class library with the following code:
namespace MyNamespace
{
public class InsecureMethodAttribute : System.Attribute { }

public interface ISecureType { }

public interface IInsecureInterface
{
[InsecureMethodAttribute]
void F();
}

class MyInterfaceImpl1 : IInsecureInterface
{
public void F() {}
}

class MyInterfaceImpl2 : IInsecureInterface, ISecureType
{
public void F() {}
}

class MyInterfaceImpl3 : ISecureType
{
public void F() {}
}
}
  1. Verify the analyzer diagnostic is not reported for MyInterfaceImpl1 and MyInterfaceImpl3, but is reported for MyInterfaceImpl2:
  1. Now, change MyInterfaceImpl2 so that it no longer implements IInsecureInterface and verify that the diagnostic is no longer reported.
class MyInterfaceImpl2 : ISecureType
{
public void F() {}
}
主站蜘蛛池模板: 滨海县| 阿鲁科尔沁旗| 南丰县| 公主岭市| 肥乡县| 阳城县| 临西县| 疏附县| 贵阳市| 孝感市| 枣强县| 佛山市| 青冈县| 阿巴嘎旗| 绥江县| 东乌珠穆沁旗| 旬邑县| 长治市| 海丰县| 通渭县| 晋江市| 纳雍县| 专栏| 太仓市| 桑植县| 奎屯市| 朝阳市| 宁明县| 建湖县| 宁国市| 普宁市| 苍溪县| 开远市| 巫溪县| 黔东| 天津市| 紫云| 萨迦县| 桑植县| 静乐县| 秦皇岛市|