C# 如何将类标记为已弃用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/314505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to mark a class as Deprecated?
提问by Mister Dev
Possible Duplicate:
How do I mark a method as Obsolete/Deprecated? - C#
可能的重复:
如何将方法标记为已过时/已弃用?- C#
How do you mark a class as deprecated? I do not want to use a class any more in my project, but do not want to delete it before a period of 2 weeks.
你如何将一个类标记为已弃用?我不想在我的项目中再使用一个类,但不想在 2 周之前删除它。
采纳答案by Patrick Desjardins
You need to use the attribute [Obsolete]
.
您需要使用属性[Obsolete]
。
This is an example:
这是一个例子:
[Obsolete("Not used any more", true)]
public class MyDeprecatedClass
{
//...
}
You do not have use parameters, they are optional (overloaded method). The first parameter is for the reason and the last one is to mark an Error in compile time instead of a warning.
您没有使用参数,它们是可选的(重载方法)。第一个参数是原因,最后一个参数是在编译时标记错误而不是警告。
回答by jjnguy
If you are using version control I would recommend just deleting the class. There is no reason to have unused code around.
如果您使用版本控制,我建议您只删除该类。没有理由留有未使用的代码。
Version control will be a handy undo if you decide later that you want the class.
如果您稍后决定需要该类,则版本控制将是一个方便的撤消操作。
回答by jjnguy
The reason to not erase a class and deprecate instead is to adhere to some "politeness policies" when your code is an estabished API and then is consumed by third parties.
不删除类并弃用的原因是当您的代码是已建立的 API 然后被第三方使用时遵守一些“礼貌策略”。
If you deprecate instead of erase, you give consumers a life cycle policy (e.g., maintenance and existence of the classes until version X.X) in order to allow them to plan a proper migration to your new API.
如果您弃用而不是擦除,您就为消费者提供了一个生命周期策略(例如,在版本 XX 之前类的维护和存在),以允许他们计划正确迁移到您的新 API。
回答by Rebecca
As per Doak's answer, but the attribute's second parameter should be set to false if you want the code to compile:
根据 Doak 的回答,但如果要编译代码,则应将属性的第二个参数设置为 false:
[Obsolete("Not used any more", false)]
public class MyDeprecatedClass
{
//...
}
This will just throw warnings.
这只会引发警告。