有什么方法可以使“ #pragma warning disable”用法更加美观?

时间:2019-05-31 11:00:22

标签: c# pragma

我在Visual Studio中启用了“将警告作为错误处理”选项。我想在我知道可以的地方忽略一些警告。因此,我使用#pragma warning disable,但我认为此指令使我的代码可读性和噪音降低。

#pragma warning disable 618
            // explanation here
            var target = new DevelopersAndGroupsCondition();
#pragma warning restore 618
            target.Set( t => t.DevelopersOnly, true );

也许存在一些更优雅的方式来忽略此警告。如果是这样,请告诉我。

1 个答案:

答案 0 :(得分:1)

There is no other way to disable a build warning on a case-by-case basis. Your only other option is to disable the warning for the entire project by adding 618 to the list of warnings to suppress in your project settings.

Visual Studio project settings

All that said, warning 618 is usage of code marked with the Obsolete attribute. If it were me, I'd remove the need to use pragma disable/restore 618 by removing references to obsolete code. When code is marked with the Obsolete attribute, there is typically an updated alternate to use, often indicated by the attribute message. You say you want to "ignore some warnings in places where I know that it's ok". If the code is explicitly marked as obsolete, are you sure it's OK, and if so, how much longer will it be before it's either not OK, or the obsolete code is removed? That's my opinion, anyway.