如何在不使用 #pragma 的情况下抑制 C# 中的编译器警告?

发布于 2024-08-29 02:27:27 字数 340 浏览 1 评论 0原文

我需要抑制 C# 中的特定编译器警告。现在我可以这样做:

#pragma warning disable 0649

private string _field;

#pragma warning restore 0649

有没有办法像下面这样做?

[SuppressCompilerWarning("0649")]
private string _field;

因为我只需要抑制该字段的警告,而不是代码块。

注意:我想抑制编译器警告,而不是代码分析警告。

谢谢!

I need to suppress a specfic compiler warning in C#. Now I can do it like this:

#pragma warning disable 0649

private string _field;

#pragma warning restore 0649

Is there a way to do it like the following?

[SuppressCompilerWarning("0649")]
private string _field;

Because I only need to suppress warnings for this field, not a code block.

Note: I want to suppress the compiler warning, not the Code-Analysis warning.

Thanks!

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

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

发布评论

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

评论(2

背叛残局 2024-09-05 02:27:27

不:

private string _field = null;

也删除警告吗?

Doesn't:

private string _field = null;

remove the warning as well?

恍梦境° 2024-09-05 02:27:27

不。您可以通过构建标志在项目范围内进行此操作,但否则字段只是另一个(小)块。

当然,你可以在某个地方给它赋一个值......这会让它高兴;-p(我假设它实际上是通过反射或其他东西赋了一个值?)

No. You can do it project wide via a build flag, but otherwise a field is just another (small) block.

Of course, you could assign it a value somewhere... that'll make it happy ;-p (I'm assuming it is actually assigned a value via reflection or something?)

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