简体   繁体   English

什么是java中的@Deprecated的.NET等价物?

[英]what is the .NET equivalent of @Deprecated in java?

.NET中是否有注释允许方法或类被弃用,以便编译器识别它们的使用和调用者(参见Java中的@Deprecated)

The [Obsolete] attribute. [Obsolete]属性。

You can add a comment on what people should be using, for example: 您可以添加对人们应该使用的内容的评论,例如:

[Obsolete("Use NewMethod() instead")]
public void OldMethod()
{
}

If you want to generate a compiler error when people use your method: 如果要在人们使用您的方法时生成编译器错误:

[Obsolete("Use NewMethod() instead", true)]
public void OldMethod()
{
}

This is an example for a method, but it can be used on almost any attribute target. 这是一个方法的示例,但它几乎可以在任何属性目标上使用。 See here for full docs. 请参阅此处获取完整文档。


Since the question was edited slightly, I'll add the VB.NET syntax as well, to cover the most common .NET languages: 由于问题略有编辑,我还将添加VB.NET语法,以涵盖最常见的.NET语言:

<Obsolete("Use NewMethod() instead")> _
Public Sub OldMethod()
End Sub

[Obsolete] - see here for documentation on it. [已淘汰] - 请参阅此处了解相关文档。 Adding this to a method or class will give a compiler warning if anyone tries to use it, with the custom message you give to the attribute if needed 如果有人试图使用它,那么将它添加到方法或类将给出编译器警告,如果需要,可以使用您给该属性的自定义消息

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

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