61

So we are trying to deprecate some of our existing classes, and have started marking them as obsolete with the ObsoleteAttribute so they will stop being used. The fact that using the KnownType attribute with a type that is marked with the Obsolete attribute and is causing a compiler warning is expected. However, in our project we have warnings treated as errors so ignoring the warning isn't an option. Is there a compiler directive to suppress this warning?

The following usage causes a compiler warning:

///ProductTemplateDataSet is marked with the Obsolete attribute
[KnownType(typeof(ProductTemplateDataSet))]
public class EntityCollectionBase : System.Data.DataSet
{
}

Edit: I understand using compiler directives to ignore errors, but this compiler warning doesn't have a number.

Pang
  • 9,564
  • 146
  • 81
  • 122
Jace Rhea
  • 4,982
  • 4
  • 36
  • 55
  • Possible duplicate of [C# - Selectively suppress custom Obsolete warnings](http://stackoverflow.com/questions/968293/c-sharp-selectively-suppress-custom-obsolete-warnings) – Kira Apr 14 '16 at 11:56

3 Answers3

123

Use this to disable the corresponding warnings just before the offending line:

#pragma warning disable 612, 618

And reenable the warnings after it:

#pragma warning restore 612, 618

Curiously enough, there're 2 warnings related to this: CS0612 and CS0618 - one is for [Obsolete] and the other for [Obsolete("Message")]. Go figure...

Jordão
  • 55,340
  • 13
  • 112
  • 144
69

If you want to avoid having to pepper your code with #prgramas, try this:
In your csproj file, find the appropriate PropertyGroup element and add

<WarningsNotAsErrors>612,618</WarningsNotAsErrors>

here's a snippet from one of my project files:

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>bin\Debug\</OutputPath>
    <DefineConstants>TRACE;DEBUG</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
    <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
    <WarningsNotAsErrors>612,618</WarningsNotAsErrors>
    <NoWarn>
    </NoWarn>
    <WarningsAsErrors>
    </WarningsAsErrors>
</PropertyGroup>

I've used this successfully with VS2010, VS2012, and VS2013 projects.

MetaFight
  • 1,295
  • 14
  • 30
  • 4
    Actually, the `` block is where you want to put it. Specifying this information in the `` block will just demote the errors back to regular warnings (you have `` set to true). – BrainSlugs83 Feb 13 '15 at 02:23
  • 9
    I don't think hiding the warning is the intention of the OP. After all, they are are deliberately adding the `Obsolete` attribute to their own code. The problem is that they also have **treat Warnings as Errors** enabled which prevents them from building their project. The solution I laid out allows them to preserve the `Obsolete` warning *and* build their project. – MetaFight Feb 13 '15 at 07:42
  • 1
    additionally add these warning to 'Suppress warnings'-box in project settings (Project -> Properties -> Build) to hide them from warnings-list – vladimir Nov 05 '15 at 22:15
  • 4
    @vladimir77, again, the point wasn't to suppress the warnings. It was to keep them as warnings while treating all other warnings as errors. – MetaFight Jan 25 '16 at 23:41
  • Not sure why this isn't "the" answer. The warnings (especially for obsolete which are liable to disappear from ABI at some point) should generally stay until resolved; the compilation failures should not. – user2864740 Feb 10 '20 at 19:09
  • @MetaFight if we don't want to use block is there any fix to this ? – NetSurfer Feb 22 '20 at 12:24
5

Could you just use a #pragma listing the appropriate warning number?

#pragma warning (C# Reference)

EDIT

Found this but it's a bit late C# - Selectively suppress custom Obsolete warnings

Community
  • 1
  • 1
MattC
  • 3,984
  • 1
  • 33
  • 49
  • Right, but what warning number? It doesn't say from the compiler output. – Jace Rhea Mar 11 '11 at 15:50
  • You could go nuclear on it; "When no warning numbers are specified, disable disables all warnings and restore enables all warnings." – MattC Mar 11 '11 at 15:53
  • "Warning as Error: 'classblah' is obsolete 'D:\Development\blah.cs'" line number 14 and column 12. – Jace Rhea Mar 11 '11 at 16:01