- 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() {}
}
推薦閱讀
- 程序員面試筆試寶典
- 認(rèn)識編程:以Python語言講透編程的本質(zhì)
- Java從入門到精通(第4版)
- Clojure for Domain:specific Languages
- Wireshark Network Security
- Apache Spark 2.x for Java Developers
- Node.js Design Patterns
- 微服務(wù)從小白到專家:Spring Cloud和Kubernetes實戰(zhàn)
- R用戶Python學(xué)習(xí)指南:數(shù)據(jù)科學(xué)方法
- 寫給程序員的Python教程
- jQuery for Designers Beginner's Guide Second Edition
- Mastering Concurrency Programming with Java 9(Second Edition)
- Java EE程序設(shè)計與開發(fā)實踐教程
- Effective C++:改善程序與設(shè)計的55個具體做法(第三版)中文版(雙色)
- 軟技能2:軟件開發(fā)者職業(yè)生涯指南