在C#中针对类的特定字段禁用/禁止警告CS0649 [英] Disable/suppress warning CS0649 in C# for a specific field of class

查看:157
本文介绍了在C#中针对类的特定字段禁用/禁止警告CS0649的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#类中有一些字段,这些字段是使用反射初始化的.编译器为他们显示CS0649警告:

I have some fields in a C# class which I initialize using reflection. The compiler shows CS0649 warning for them:

字段 foo'从未分配,并且将始终具有其默认值值 null"(CS0649)(Assembly-CSharp)

Field foo' is never assigned to, and will always have its default valuenull' (CS0649) (Assembly-CSharp)

我只想对这些特定字段禁用警告,并且仍然让其他类和该类的其他字段显示警告.可以为整个项目禁用CS0649,还有其他更细粒度的方法吗?

I'd like to disable the warning for these specific fields only and still let the warning be shown for other classes and other fields of this class. It is possible to disable CS0649 for the whole project, is there anything more fine-grained?

推荐答案

您可以使用

You could use #pragma warning to disable and then re-enable particular warnings:

public class MyClass
{
    #pragma warning disable 0649

    // field declarations for which to disable warning
    private object foo;

    #pragma warning restore 0649

    // rest of class
}

请参阅在C#中取消从不使用"和从不分配给"警告,以获取扩展的答案.

Refer to Suppressing "is never used" and "is never assigned to" warnings in C# for an expanded answer.

这篇关于在C#中针对类的特定字段禁用/禁止警告CS0649的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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