问题
Is currently a way to extend Roslyn compilation and add some custom rule set that will break a build and show in ErrorList of VisualStudio?
I search StackOverflow but there is no answer that works - maybe recently something came up in this topic or maybe there is other way to do this without roslyn?
回答1:
Roslyn – custom build error extension
Just like Slaks and JoshVarty said, this is an analyzer feature. To create your custom Roslyn analyzer rule, you can check this MS tutorial:
C# and Visual Basic - Use Roslyn to Write a Live Code Analyzer for Your API.
In this document, you can find following description:
In the line declaring the Rule field, you can also update the severity of the diagnostics you’ll be producing to be errors rather than warnings. If the regex string doesn’t parse, the Match method will definitely throw an exception at run time, and you should block the build as you would for a C# compiler error. Change the rule’s severity to DiagnosticSeverity.Error:
internal static DiagnosticDescriptor Rule = new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, Category, DiagnosticSeverity.Error, isEnabledByDefault: true, description: Description);
This will cause the build to break.
And
In order to make the build fail for the rules, you need to add the analyzer as a nuget package to the project. This will ensure that failures will cause the build to fail as expected.
Certification:Roslyn Analyzer Rule does not fail the build.
Hope this helps.
来源:https://stackoverflow.com/questions/51829187/roslyn-custom-build-error-extension