C# - 有选择地抑制自定义过时警告 [英] C# - Selectively suppress custom Obsolete warnings

查看:10
本文介绍了C# - 有选择地抑制自定义过时警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果使用某种方法,我正在使用 Obsolete 属性(正如其他程序员建议的那样)来显示警告.

I'm using the Obsolete attribute (as just suggested by fellow programmers) to show a warning if a certain method is used.

有没有办法在合理使用的地方抑制类似于 CodeAnalysis'SuppressMessage 的警告?

Is there a way to suppress the warning similar to CodeAnalysis' SuppressMessage at points where the use is justified?

这需要适用于 [Obsolete("Some message")] ,它生成警告 618 和普通的 [Obsolete] 属性,没有消息生成警告 612.

This needs to work for [Obsolete("Some message")] which generates warning 618 and the plain [Obsolete] attribute with no message which generates warning 612.

推荐答案

Use #pragma warning disable:

using System;

class Test
{
    [Obsolete("Message")]
    static void Foo(string x)
    {
    }

    static void Main(string[] args)
    {
#pragma warning disable 0618
        // This one is okay
        Foo("Good");
#pragma warning restore 0618

        // This call is bad
        Foo("Bad");
    }
}

事后恢复警告,以免错过坏"电话.

Restore the warning afterwards so that you won't miss "bad" calls.

这篇关于C# - 有选择地抑制自定义过时警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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