Visual Studio 中作为错误的警告与已弃用的属性

发布于 2024-10-10 12:25:33 字数 301 浏览 5 评论 0 原文

我们喜欢警告作为错误设置,因为我们有一项政策,即不签入带有警告的代码,这是我们发现执行该政策的唯一有效方法。

我们还喜欢使用过时属性来标记不应再使用的方法。

问题是,向方法或类添加 Obsolete 属性会立即导致大量项目无法构建(更不用说不推荐使用 .NET API 调用时出现的问题)。

有人对此有好的解决方案吗?

我们需要一个可见的、难以忽略的指示器,表明您正在使用已弃用的 API,但不会导致构建失败。我们希望在 IDE 和 CI 构建中看到警告。

We like the Warnings as Errors setting as we have a policy of not checking in code with warnings and this is the only effective way we have found to enforce it.

We also like to use the Obsolete attribute to flag methods that should not be used any more.

Problem is that adding a Obsolete attribute to a method or class immediately causes tons of projects to not build (not to mention problems if a .NET API call is deprecated).

Does anyone have a good solution to this?

We want a visible, hard-to-ignore indicator that you are using a deprecated API but that does not cause the build to fail. We want to see the warnings in the IDE and in CI builds.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

命比纸薄 2024-10-17 12:25:33

一个简单的解决方案是使用构建配置(例如调试构建配置),而不将警告视为错误。然而,如果重点是向您的开发人员标记构建过程中出现问题,那就不好了,因为他们会忘记在签入之前进行发布构建。

或者,您可以设置“警告作为错误”,而不是使用“警告作为错误”建立你的规则集来引发错误本身而不是发出警告。当然,这意味着非 CA 警告不会导致错误。

我认为最好的解决方案是在服务器端处理它。理想情况下,您应该有某种门控签入,以便您的代码存储库拒绝未使用其构建定义进行构建的提交(打开警告作为错误,并且您的开发人员可以关闭警告作为错误)。我怀疑这是 TFS-2k10 独有的功能。

A simple solution would be to have a build configuration (e.g. your debug build configuration) without warnings as errors. If, however, the point is to flag to your developers that something is wrong on build, that's no good as they'll forget to do a release build before they check in.

Alternately, rather than using "warnings as errors" you could set up your ruleset to throw errors itself rather than raise warnings. This will mean, of course, that non-CA warnings won't cause errors.

The best solution, I think, would be to handle it on the server side. Ideally you'd have some sort of gated checkin so that your code repository rejects commits that don't build using its build definition (with warnings-as-errors on, and your developers can leave warnings-as-errors off). I suspect that's a TFS-2k10-only feature though.

笑忘罢 2024-10-17 12:25:33

其他堆栈溢出帖子应该有所帮助:
https://stackoverflow.com/a/468166/9195608

基本上它说:

您可以在项目文件中添加一个WarningsNotAsErrors标签。

<PropertyGroup>
    ...
    ...
    <WarningsNotAsErrors>612,618</WarningsNotAsErrors>
</PropertyGroup>

注意:612和618都是关于Obsolete的警告

612和618的区别在于ObsoleteAttribute的注释。不带注释的 ObsoleteAttribute 会生成错误 612,带注释的 ObsoleteAttribute 会生成错误 618。

This other stack overflow post should help:
https://stackoverflow.com/a/468166/9195608

Basically it says:

You can add a WarningsNotAsErrors-tag in the project file.

<PropertyGroup>
    ...
    ...
    <WarningsNotAsErrors>612,618</WarningsNotAsErrors>
</PropertyGroup>

Note: 612 and 618 are both warnings about Obsolete

The difference between 612 and 618 is the comment of the ObsoleteAttribute. An ObsoleteAttribute without comment generates the error 612, and one with a comment generates 618.

如梦亦如幻 2024-10-17 12:25:33

如此处所述 /sdl(启用附加安全检查),如果关闭 SDL,编译器会将其视为警告。

As explained here /sdl (Enable Additional Security Checks), if you switch off SDL the compiler will treat it as a warning.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文