C# - 选择性燮preSS定制过时的警告 [英] C# - Selectively suppress custom Obsolete warnings

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

问题描述

我使用了过时属性(如刚才的同胞程序员的建议)显示警告如果采用一定的方法。

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

有没有办法用晚餐preSS警告类似codeAnalysis燮pressMessage 在点使用是合理的在哪里?

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

感谢您!

修改

这是 [过时(一些信息)] ,因为我需要包括有关警告的一些细节。然而,的#pragma警告禁用612 一旦我添加的消息裸 [作废] 属性不工作...

This is for [Obsolete("Some message")] as I need to include some details about the warning. However, #pragma warning disable 612 doesn't work anymore once I add the message to the naked [Obsolete] attribute...

编辑2

找到了正确的警告数量 - 这是的 618 如果您有以下过时的属性信息

Found the right warning number - It is 618 if you have a message following the obsolete attribute.

所以,做我想做的事:

的#pragma警告禁用618

,然后呼叫后

的#pragma警告恢复618

由于贾里德面值和乔恩斯基特指着我在正确的方向!

Thanks to Jared Par and Jon Skeet for pointing me in the right direction!

推荐答案

使用的#pragma警告禁用

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# - 选择性燮preSS定制过时的警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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