你怎么可以标记代码"不以备将来使用" [英] How can you mark code as "not for future use"

查看:163
本文介绍了你怎么可以标记代码"不以备将来使用"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常在我想继续使用的方法或类劝阻其他开发商的情况下结束。举例来说,假设我有两个库方法A和B,其中A是做一些任务和B的旧的方式是这样做的任务,新的方式。在许多情况下,A和B是十分不同使用A开始使用乙类非小事,使重构代码(例如流经的附加状态需要)。由于A工作在它的使用情况下,我不希望到重构的优先级。我这样做,不过,想给我的同胞开发商的视觉指示A是不是在新的代码中使用。

I often end up in a situation where I want to discourage other developers from continuing to use a method or class. For example, let's say I have two library methods "A" and "B" where "A" is the "old" way of doing some task and "B" is the "new" way of doing that task. In many cases, A and B are sufficiently different to make refactoring code using A to start using B non-trivial (e. g. requires flowing through of additional state). Since A works in the cases where it's used, I don't want to prioritize the refactor. I do, however, want to give my fellow developers a visual indication that A is not to be used in new code.

因此,我最想要的罢工,通过引用与 ObsoleteAttribute 的成员时,如果没有你相关的编译器警告/错误(因为转弯上会发出错误的数百名来自A的所有旧的使用,我们不打算很快解决任何时间)。这样,如果一个开发人员编写的一个代码的新行,他或她将立即通知罢工通和修复使用B中的代码。

Thus, I'd ideally like the strike-through you get when referencing a member with the ObsoleteAttribute WITHOUT the associated compiler warning/error (since turning that on would emit hundreds of errors from all the old uses of A that we don't plan to address any time soon). That way, if a developer writes a new line of code with A, he or she will immediately notice the strike-through and fix the code to use B.

有?在VisualStudio中的任何方式来获得这样的功能(2012)

Is there any way to get such functionality in VisualStudio (2012)?

编辑:


  • 有过一些意见以没有办法,新旧代码来区分的效果。我同意。但是,这不是我所要求的,所以让我澄清:不是,我要的是代码的可视化表示是过时的没有相应的编译器警告或错误(例如删除线。)。这样一来,开发商在阅读旧代码或编写新代码的过程中会得到直接指示的东西是过时的。即使这不是在.NET中本地支持,也许有一个扩展VS在那里为这个目的?

  • There have been several comments to the effect of "there's no way to distinguish between new and old code". I agree. However, that's not what I'm asking for, so let me clarify: instead, what I want is a visual representation of code being "out of date" (e. g. strikethrough) without the corresponding compiler warning or error. That way, developers in the process of reading old code or writing new code will get an immediate visual indication that something is out of date. Even if this isn't supported natively in .NET, maybe there is a VS extension out there for this purpose?

有过一些意见,大意的你不能都有一个警告,而不是有一个警告。我以为我上面的解释用例,但我会再试试看。我们有一个核心库的集合这是大量使用整个构成我们的代码库的各种解决方案。有时候,我做一个更新这些库提供了一个新的,更好的API,用于执行一些任务之一。为了保持向后兼容性,我不能只是删除这样做的任务(在许多情况下)的老办法,因为吨的现有代码依赖于使用旧的API集,不能平凡重构以使用新的。此外,没有迫切的理由这样做;它只是引入风险虫子到现有代码。不过,我想在视觉上提醒开发商,某些原料药应当有利于其他的人来避免的事实某种方式。这是困难的,因为开发商往往通过阅读现有的代码,完成同样的任务,以了解如何完成一些任务。这使得新的API难普及,因为旧的根深蒂固的API是由这么多的现有代码引用。通过编译器警告 ObsoleteAttribute 实现这一目标,但这些警告将刚刚从数百旧的API现有用途的创建吨的噪音。这就是为什么我喜欢的删除线:它的东西,是非常直观的,但它只能在当他或她在读取或写入使用过时的API代码开发者闯入。下面是我想要的地方标志着一个旧的API变化的一些例子:

There have been several comments to the effect of "you can't both have a warning and not have a warning". I thought I explained the use case above, but I'll give it another try. We have a set of core libraries which are used heavily throughout the various solutions that comprise our code base. Sometimes, I make an update to one of these libraries which provides a new, better API for performing some task. To maintain backwards compatibility, I can't just remove the old way of doing that task (in many cases), since tons of existing code relies on using the old set of APIs and can't be trivially refactored to use the new one. Furthermore, there's no pressing reason to do so; it would just risk introducing bugs into existing code. However, I'd like some way of visually alerting developers to the fact that certain APIs should be avoided in favor of other ones. This is difficult, since developers tend to learn how to accomplish some task by reading existing code that accomplishes the same task. That makes new APIs hard to spread, since the old entrenched APIs are referenced by so much existing code. The ObsoleteAttribute achieves this via compiler warnings, but those warnings will just create tons of noise from the hundreds of existing uses of the old APIs. That's why I like the strikethrough: it's something that is very visual and yet it will only intrude on a developer when he or she is reading or writing code that uses an out of date API. Here are some examples of changes where I wanted to mark an old API:


  • 我们引入了一个新的API来运行SQL查询是更简洁,少古怪,而且比我们以前有更加灵活。很难彻底删除旧的API,因为它有可能会依靠这么多古怪的行为。不过,我想推动人民对未来发展的新的API。

  • 我们有单元测试帮手的API的两个内部集。年长的一个是完美的功能,但它依赖于继承和不是很灵活。将更新的一个内置使用属性和更灵活。数以百计的老测试运行仍然使用旧的API,但我要推的新的考验作家使用新的API。

  • 我们的核心库有一些旧随机遗留代码不真正适合但将很难在这一点上去除。我想削减对这些类型和方法新引用添加。这样一来,就可能成为成本效益在某些时候是依赖于他们现有的代码删除它们消失,由于正常的流失。

作为进一步的说明,我想答案来这个问题确实说明了为什么您可能没有标注过时的东西,即使你不建议在新代码中使用它的一个好工作。

As a further note, I think the answer to this question does a good job of describing why you might not mark something obsolete even though you wouldn't recommend using it in new code.

有几种意见/回答简单地调用出来的 ObsoleteAttribute 的存在。请注意,这个问题的文字一直引用的属性。

There are several comments/answers simply calling out the existence of the ObsoleteAttribute. Please note that the text of this question has always referenced that attribute.

推荐答案

添加href=\"http://msdn.microsoft.com/en-us/library/system.obsoleteattribute%28v=vs.110%29.aspx\" rel=\"nofollow\">过时属性

Adding the Obsolete attribute to your method will give the strikethrough in intellisense.

[ObsoleteAttribute("This property is obsolete. Use NewProperty instead.", false)] 
public static string OldProperty
{ get { return "The old property value."; } }

要禁用警告属性之前补充一点:

To disable the warnings add this before the attribute:

#pragma warning disable 612, 618

而重新启用:

#pragma warning restore 612, 618

如前所述这里,把你的项目文件中忽略,而不是在你的代码将是一个非常干净的解决方案。

As noted here, putting a ignore in your project file instead of in your code would be a very clean solution.

<WarningsNotAsErrors>618</WarningsNotAsErrors>



编辑:此外,检查出@ JonHanna的有关使用的 EditorBrowsable 属性

正如其他人指出,实际上有2警告抛出与过时的属性。

As others have noted, there are actually 2 warnings that are thrown with the obsolete attribute.

编辑:

#pragma warning disable 612, 618
[Obsolete]
#pragma warning restore 612, 618
public class test1
{...

当您尝试使用测试1 您将获得:

When you try to use test1 you will get:

请注意,当你键入 VAR测试=新TEST1()删除线不会发生。

Note that when you type var test = new test1() the strikethrough does not occur.

test1的测试=新TEST1()将显示删除线。

这篇关于你怎么可以标记代码&QUOT;不以备将来使用&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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