在与 KnownType 属性一起使用的类上使用 Obsolete 属性时如何忽略编译器警告

How to ignore compiler warning when using Obsolete attribute on a class used with a KnownType attribute(在与 KnownType 属性一起使用的类上使用 Obsolete 属性时如何忽略编译器警告)
本文介绍了在与 KnownType 属性一起使用的类上使用 Obsolete 属性时如何忽略编译器警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我们正在尝试弃用一些现有的类,并已开始使用 ObsoleteAttribute 将它们标记为已过时,以便停止使用它们.预期将 KnownType 属性与标记为 Obsolete 属性的类型一起使用并导致编译器警告的事实.但是,在我们的项目中,我们将警告视为错误,因此不能选择忽略警告.是否有一个编译器指令来抑制这个警告?

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
{
}

我理解使用编译器指令来忽略错误,但是这个编译器警告没有数字.

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

推荐答案

使用它来禁用违规行之前的相应警告:

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

#pragma warning disable 612, 618

然后重新启用警告:

#pragma warning restore 612, 618

奇怪的是,有 2 个与此相关的警告:CS0612 和 CS0618 - 一个用于 [Obsolete],另一个用于 [过时的(消息")].去图...

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

这篇关于在与 KnownType 属性一起使用的类上使用 Obsolete 属性时如何忽略编译器警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)