Roslyn – 自定义构建错误扩展 [英] Roslyn – custom build error extension

查看:59
本文介绍了Roslyn – 自定义构建错误扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前是否有一种方法可以扩展 Roslyn 编译并添加一些自定义规则集,这些规则集会破坏构建并显示在 VisualStudio 的 ErrorList 中?

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?

我搜索 StackOverflow,但没有有效的答案 - 也许最近在这个主题中出现了一些东西,或者没有 roslyn 有其他方法可以做到这一点?

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?

推荐答案

Roslyn – 自定义构建错误扩展

Roslyn – custom build error extension

正如 Slaks 和 JoshVarty 所说,这是一个分析器功能.要创建自定义 Roslyn 分析器规则,您可以查看此 MS 教程:

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# 和 Visual Basic - 使用 Roslyn 为您编写实时代码分析器API.

在本文档中,您可以找到以下说明:

In this document, you can find following description:

在声明 Rule 字段的行中,您还可以更新严重性您将产生的诊断结果是错误而不是警告.如果正则表达式字符串没有解析,Match 方法将在运行时肯定会抛出异常,你应该阻止像 C# 编译器错误一样构建.更改规则的严重性到 DiagnosticSeverity.Error:

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);

这将导致构建中断.

为了使规则的构建失败,您需要添加分析器作为项目的 nuget 包.这将确保失败将导致构建按预期失败.

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.

认证:Roslyn 分析器规则不会使构建失败.

希望这会有所帮助.

这篇关于Roslyn – 自定义构建错误扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆