views:

1438

answers:

2

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 warning similar to CodeAnalysis' SuppressMessage at points where the use is justified?

Thank you!

EDIT

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...

EDIT 2

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

So to do what I want to do:

#pragma warning disable 618

and then after the call

#pragma warning restore 618

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

+2  A: 

You're looking for the #pragma warning disable directive

Essentially you add the following command above the call site in the .cs file.

#pragma warning disable 612
SomeMethodCall

612 is the error message ID for calling obsolete methods

JaredPar
This only works if I don't give a message with my Obsolete attribute. I have a message though like this: [Obsolete("This is why this shouldnt be used - use XYZ instead.")]. Once I put the message in, the pragma warning disable 612 stops working and I'm getting warnings regardless. Do I need another error ID instead maybe?
Alex
Found it - The correct warning number is 618 if there's a message in the Obsolete attribute.
Alex
+12  A: 

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.

Jon Skeet
This only works if I don't give a message with my Obsolete attribute. I have a message though like this: [Obsolete("This is why this shouldnt be used - use XYZ instead.")]. Once I put the message in, the pragma warning disable 612 stops working and I'm getting warnings regardless. Do I need another error ID instead maybe?
Alex
Found it - The correct warning number is 618 if there's a message in the Obsolete attribute. Thank you!
Alex
Goodo - I've adjusted my example to match this.
Jon Skeet