- Roslyn Cookbook
- Manish Vasani
- 226字
- 2021-07-15 17:07:34
How to do it...
- In Solution Explorer, double click on Resources.resx file in CSharpAnalyzers project to open the resource file in the resource editor.
- Replace the existing resource strings for AnalyzerDescription, AnalyzerMessageFormat and AnalyzerTitle with new strings.

- Replace the Initialize method implementation with the code from CSharpAnalyzers/CSharpAnalyzers/CSharpAnalyzers/DiagnosticAnalyzer.cs/ method named Initialize.
- 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.
- Click on Ctrl + F5 to start a new Visual Studio instance with the analyzer enabled.
- 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

- 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() {}
}
}
- Verify the analyzer diagnostic is not reported for MyInterfaceImpl1 and MyInterfaceImpl3, but is reported for MyInterfaceImpl2:

- 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() {}
}
推薦閱讀
- Implementing Modern DevOps
- 潮流:UI設計必修課
- Effective C#:改善C#代碼的50個有效方法(原書第3版)
- 機械工程師Python編程:入門、實戰與進階
- Python高效開發實戰:Django、Tornado、Flask、Twisted(第3版)
- Python貝葉斯分析(第2版)
- Mastering JavaScript High Performance
- BIM概論及Revit精講
- 區塊鏈技術與應用
- 第一行代碼 C語言(視頻講解版)
- 動手學數據結構與算法
- Android驅動開發權威指南
- Spring Boot實戰
- 零基礎學HTML+CSS
- PHP+MySQL動態網站開發從入門到精通(視頻教學版)