如何将一种方法标记为过时或过时? [英] How to mark a method as obsolete or deprecated?

查看:88
本文介绍了如何将一种方法标记为过时或过时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


如何使用 C#将方法标记为过时或过时?

How do I mark a method as obsolete or deprecated using C#?


推荐答案

最简单的方法是添加 ObsoleteAttribute 作为方法的属性。确保包括适当的解释:

The shortest way is by adding the ObsoleteAttribute as an attribute to the method. Make sure to include an appropriate explanation:

[Obsolete("Method1 is deprecated, please use Method2 instead.")]
public void Method1()
{ … }

也可以使编译失败,如果从如下代码中的某处调用了该方法,则将该方法的使用视为 错误而不是警告

You can also cause the compilation to fail, treating the usage of the method as an error instead of warning, if the method is called from somewhere in code like this:

[Obsolete("Method1 is deprecated, please use Method2 instead.", true)]

这篇关于如何将一种方法标记为过时或过时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆