将本机方法标记为已弃用/过时?
在 .NET 中,您可以将某些方法标记为已过时,以便开发人员在尝试使用已弃用的方法时收到警报。
<Obsolete("Do not call this method.")> Private Sub FormerMethod()
问题是你只能在你控制的类中执行此操作。当您希望开发人员避免在 .NET 本地提供的或由供应商提供的类上使用某些方法时,您会怎么做?
例如,如果您希望开发人员更喜欢 DataTable
上的某些自定义扩展方法而不是 Select
,该怎么办?如果只是为了弃用 Select
,我不想定义 DataTable
类的自定义版本。这将使我们不得不监视定制表是否正在被使用。
我知道有像 (FxCop) 这样的工具可以帮助执行编码标准;然而,我的问题是,如果没有第三方工具,是否可以解决这个问题。
In .NET you can mark certain methods as obsolete so that developers are alerted when they attempt to use the deprecated method.
<Obsolete("Do not call this method.")> Private Sub FormerMethod()
The thing is you can only do this within classes you control. What do you do when you want your developers to avoid using certain methods on classes provided natively in .NET or by a vendor?
For example, what if you want your developer to prefer some custom extension method on DataTable
rather than Select
. I'd hate to have to define a custom version of the DataTable
class if only to deprecate Select
. That would leave us having to police whether or not the custom table was being used.
I'm aware that there are tools like (FxCop) that help enforce coding standards; however, my question is whether or not this can be handled without some third party tool.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
虽然您可能不喜欢这个答案,但我认为包装是合适的选择。
对于第三方供应商的代码,包装应该是强制性的,只是为了产生单元测试的抽象级别。作为一个包装器,您显然可以将方法标记为
已过时
。对于 .NET 对象,这有点棘手。显然,某人可以只包含名称空间并进入城镇;我不相信有像您想要的那样的“仅代码”解决方案,除了可能进行集成测试来搜索您的解决方案并在调用您不喜欢的特定方法时失败。
While you may not like the answer, I would think wrapping would be the appropriate choice.
For a third party vendor's code, wrapping should be mandatory just to produce a level of abstraction for unit testing. And being a wrapper, you can obviously mark methods as
Obsolete
.For .NET objects, it's a little more tricky. Obviously, someone can just include the namespace and go to town; I don't believe there is a 'code-only' solution to this problem like you want, other than perhaps making an integration test that searches over your solution and fails when that particular method you dont like is called.
如果您想将 .Net Framework 类标记为已弃用,您可以在每个人都使用的共享\基础设施类之一中重新定义它。
示例:
也可以通过扩展原始 .Net Framework 类,然后重写调用基方法但用
Obsolete
属性修饰的特定方法来完成特定方法。但是,您需要解析循环类定义(可能通过使用程序集引用别名)。
If you want to mark a .Net Framework class as deprecated you can redefine it in one of the shared\infrastructure classes everyone is using.
Example:
It might be done also for specific methods by extending the original .Net Framework class, and then overriding specific methods that call the base method but are decorated with the
Obsolete
attribute.However, you'll need resolve the circular class definition (maybe by using assembly reference alias).