1

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?

dnf
  • 1,659
  • 2
  • 16
  • 29
  • 1
    Can you describe the custom rule? It's probably possible to create a Roslyn Analyzer that issues errors (`DiagnosticSeverity.Error`) when your rule is broken. https://github.com/dotnet/roslyn/wiki/How-To-Write-a-C%23-Analyzer-and-Code-Fix – JoshVarty Aug 13 '18 at 19:42
  • I have to force methods to be virtual due the interceptor to work - I was thinking about analyzers but main question is about breaking a build and show in error list. Can I do that? – dnf Aug 13 '18 at 19:51
  • 1
    That's exactly what analyzers do. – SLaks Aug 14 '18 at 00:22

1 Answers1

2

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.

Leo Liu
  • 71,098
  • 10
  • 114
  • 135