C# - Selectively suppress custom Obsolete warnings

后端 未结 4 1731
小鲜肉
小鲜肉 2020-11-28 05:04

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

Is there a way to suppress the

4条回答
  •  失恋的感觉
    2020-11-28 05:23

    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.

提交回复
热议问题