Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
426 views
in Technique[技术] by (71.8m points)

c# - 如何将方法标记为过时或已弃用?(How to mark a method as obsolete or deprecated?)

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

  ask by community wiki translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The shortest way is by adding the ObsoleteAttribute as an attribute to the method .

(最简单的方法是将ObsoleteAttribute作为方法属性添加。)

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)]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...