How to ignore compiler warning when using Obsolete attribute on a class used with a Knowntype attribute

前端 未结 3 1419
清酒与你
清酒与你 2020-12-08 01:42

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

相关标签:
3条回答
  • 2020-12-08 02:31

    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

    0 讨论(0)
  • 2020-12-08 02:38

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

    0 讨论(0)
  • 2020-12-08 02:40

    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.

    0 讨论(0)
提交回复
热议问题