简体   繁体   English

在 C# 中禁用 OBSOLETE 错误

[英]Disabling OBSOLETE error in C#

I am using the Microsoft TFS API and one of the properties on one of the interfaces has been marked as Obsolete and it instructs me to use a different property.我正在使用 Microsoft TFS API,其中一个接口上的一个属性已标记为过时,它指示我使用不同的属性。 Unfortunately the property the API wants me to use is used by TFS2010 and not TFS2008.不幸的是,API 希望我使用的属性由 TFS2010 而不是 TFS2008 使用。

I have tried doing this:我试过这样做:

#pragma warning disable 0612, 0618
            request.CommandLineArguments = arguments;
#pragma warning restore 0612, 0618

But I still get the error that CommandLineArguments is obsolete.但我仍然收到 CommandLineArguments 已过时的错误。 Is there anyway to suppress this?有没有办法压制这个?

EDIT编辑

Unfortunately this is not showing up as a 'Warning as Error', in fact Treat Warning's As Error's is turned off in my project.不幸的是,这并没有显示为“警告为错误”,实际上在我的项目中将警告视为错误已关闭。 Here is a screen cap of the offending code as well as the error list这是违规代码的屏幕截图以及错误列表

在此处输入图片说明

EDIT 2:编辑2:

After using ILSpy the CommandLineArguments property looks like this in the TFS2010 API:使用 ILSpy 后,TFS2010 API 中的 CommandLineArguments 属性如下所示:

    [Obsolete("This property has been deprecated. Please remove all references. To pass command line arguments to MSBuild.exe, set the ProcessParameters property.", true)]
    string CommandLineArguments
    {
        get;
        set;
    }

Unfortunately I don't think there is a way to tell the compiler to ignore the error that the Obsolete attribute is causing.不幸的是,我认为没有办法告诉编译器忽略 Obsolete 属性导致的错误。

EDIT 3 As @Peter Ritchie points out this value could be set via reflection.编辑 3 正如@Peter Ritchie 指出的,这个值可以通过反射来设置。 As I thought through this problem though my guess is that if Microsoft set the property to throw an exception even if you did set it via reflection I doubt that the value would be referenced anywhere.正如我对这个问题的思考,尽管我的猜测是,如果 Microsoft 将属性设置为抛出异常,即使您确实通过反射设置了它,我怀疑该值是否会在任何地方被引用。

Following works for me:以下对我有用:

#pragma warning disable 612,618
            request.CommandLineArguments = arguments;
#pragma warning restore 612,618

notice no leading 0 in the numbers注意数字中没有前导 0

EDIT: Okay, your assembly has the "true" argument in the ObsoleteAttribute constructor.编辑:好的,您的程序集在 ObsoleteAttribute 构造函数中具有“true”参数。 This means you can't use the property and not get an error.这意味着您不能使用该属性并且不会收到错误消息。

If you can't re-write your code to avoid using this property, you'll have to invoke the property setter via reflection, for example:如果您无法重新编写代码以避免使用此属性,则必须通过反射调用属性设置器,例如:

request.GetType().GetProperty("Number").SetValue(request, arguments, null);

and getting is similar:和得到是相似的:

(string)request.GetType().GetProperty("CommandLineArguments").GetValue(request, null); (string)request.GetType().GetProperty("CommandLineArguments").GetValue(request, null);

Visual Studio 2015视觉工作室 2015

Build failing due to [Obsolete]?由于 [Obsolete] 导致构建失败?

This would only occur if "Treat Warnings As Errors" is enabled, and there is a method with the [Obsolete] attribute.仅当启用“将警告视为错误”时才会发生这种情况,并且存在具有 [Obsolete] 属性的方法。

Method 1: Downgrade error to warning方法一:将错误降级为警告

Add <WarningsNotAsErrors>612,618</WarningsNotAsErrors> in the .csproj file (repeat for all sections):.csproj文件中添加<WarningsNotAsErrors>612,618</WarningsNotAsErrors> (对所有部分重复):

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

If dealing with many .csproj files , see Appendix A: Notepad++ for search and replace .如果处理许多.csproj files ,请参阅Appendix A: Notepad++ for search and replace

Method 2: Ignore error in file方法 2:忽略文件中的错误

Note: This method is not recommended, because it hides the warnings for methods marked [Obsolete].注意:推荐使用此方法,因为它隐藏了标记为 [Obsolete] 的方法的警告。 We still want to see a list of all calls to obsolete methods so we can upgrade them.我们仍然希望看到对过时方法的所有调用的列表,以便我们可以升级它们。

Use #pragma warning disable 612,618使用#pragma warning disable 612,618

Method 3: Ignore error in project方法 3:忽略项目中的错误

Note: This method is not recommended, because it hides the warnings for methods marked [Obsolete].注意:推荐使用此方法,因为它隐藏了标记为 [Obsolete] 的方法的警告。 We still want to see a list of all calls to obsolete methods so we can upgrade them.我们仍然希望看到对过时方法的所有调用的列表,以便我们可以升级它们。

Edit the project (repeat for all sections):编辑项目(对所有部分重复):

在此处输入图片说明

Method 4: Ignore error in project方法 4:忽略项目中的错误

Note: This method is not recommended, because it hides the warnings for methods marked [Obsolete].注意:推荐使用此方法,因为它隐藏了标记为 [Obsolete] 的方法的警告。 We still want to see a list of all calls to obsolete methods so we can upgrade them.我们仍然希望看到对过时方法的所有调用的列表,以便我们可以升级它们。

Manually edit your .csproj to disable warnings for specific errors.手动编辑 .csproj 以禁用针对特定错误的警告。 Add the tag <NoWarn>612,618</NoWarn> (repeat for all sections):添加标签<NoWarn>612,618</NoWarn> (对所有部分重复):

<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
    <NoWarn>612,618</NoWarn>
    <DebugSymbols>true</DebugSymbols>
    <OutputPath>bin\x64\Debug\</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
    <DebugType>full</DebugType>
    <PlatformTarget>x64</PlatformTarget>
    <ErrorReport>prompt</ErrorReport>        
</PropertyGroup>

Appendix A: Notepad++ for search and replace附录 A:Notepad++ 用于搜索和替换

Have a lot of files?有很多文件吗? No problem!没问题!

Open all .csproj files in NotePad++, then:在 NotePad++ 中打开所有.csproj文件,然后:

  • Find: <TreatWarningsAsErrors>true</TreatWarningsAsErrors>查找: <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
  • Replace: <TreatWarningsAsErrors>true</TreatWarningsAsErrors>\\n\\t<WarningsNotAsErrors>612,618</WarningsNotAsErrors>替换: <TreatWarningsAsErrors>true</TreatWarningsAsErrors>\\n\\t<WarningsNotAsErrors>612,618</WarningsNotAsErrors>

在此处输入图片说明

Just in case anyone else stumbles on this.以防万一其他人偶然发现了这一点。

If you mark the method in which you set the property as Obsolete and DONT mark it as true the compiler will ignore the interior error throwing your higher level warning instead which you can ignore.如果您将设置属性的方法标记为 Obsolete 并且不要将其标记为 true,则编译器将忽略内部错误,并抛出您可以忽略的更高级别的警告。

IE IE

[Obsolete("Cause it aint",false)]
public void Foo(object arguments)
{
     request.CommandLineArguments = arguments;
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM