Prevent ObsoleteAttribute warning bubbling up call stack

I have a class using a deprecated assembly (System.Data.OracleClient). At compile time this results in a message 'MethodName()' is obsolete: 'OracleConnection has been deprecated. We can't replace this assembly and we are now trying to remove all warnings generated at compile time so that new warnings are clear to us.

It looks like whenever I try to suppress the warning using [System.ObsoleteAttribute("OracleConnection has been deprecated. http://go.microsoft.com/fwlink/?LinkID=144260", false)] the warning just gets pushed up the stack to the calling method.

In the example class below only method E() is using the deprecated assembly but I get a warning in method A() as it is the highest method in the stack without an ObsoleteAttribute. If I remove the attribute from C() then the warning gets pushed down the stack to C().

public class OracleCaller
{
    public void A()
    {
        C();
    }
    [System.ObsoleteAttribute("OracleConnection has been deprecated. http://go.microsoft.com/fwlink/?LinkID=144260", false)]
    private void C()
    {
        D();
    }

    [System.ObsoleteAttribute("OracleConnection has been deprecated. http://go.microsoft.com/fwlink/?LinkID=144260", false)]
    private void D()
    {
        E();
    }
    [System.ObsoleteAttribute("OracleConnection has been deprecated. http://go.microsoft.com/fwlink/?LinkID=144260", false)]
    private void E()
    {
        System.Data.OracleClient.OracleCommand c = new System.Data.OracleClient.OracleCommand();
    }
}

In my application this will lead me to have to decorate methods with this attribute all the way up to the UI which seems pointless.

Can I get round this so that I only suppress the warning in the method where the assembly is called?

Thanks

Jon Skeet
people
quotationmark

Can I get round this so that I only suppress the warning in the method where the assembly is called?

Yes - by using the normal way of suppressing warnings in C# - using #pragma warning disable.

Here's an example:

using System;

public class Test
{    
    static void Main()
    {
#pragma warning disable 0618       
        ObsoleteMethod();
#pragma warning restore 0618        
    }

    [Obsolete("Don't call me")]
    static void ObsoleteMethod()
    {
    }
}

people

See more on this question at Stackoverflow