将新类添加到框架命名空间是一个好习惯吗?
很久以前,我记得读过 Microsoft 的一个非常强烈的建议,反对将自己的类添加到框架命名空间中。我一直在寻找它但没有成功。
我记得的主要原因是该框架的后续版本可能会出现类名冲突。
还有其他原因吗?您是否会建议将您自己的类添加到框架命名空间中?关于此事是否有任何现有的官方指导?
A long time ago, I remember reading a quite strong recommendation from Microsoft against adding your own classes to framework namespaces. I've been unsuccessfully searching for it.
The main reason I remember is that a subsequent version of the framework might ship with a class name collision.
Are there any other reasons? Would you ever recommend adding your own classes to a framework namespace? Is there any extant official guidance on the matter?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您尝试存根不再/尚未存在的内容时,您希望在不属于您的命名空间中拥有一个类,但唯一的情况是在您不想更改的代码库中使用。
例如,我编写了一个 HashSet 实现,并将其放置在一个应用程序的 System.Collections.Generic 命名空间中,该应用程序在 .NET 3.5 处于测试阶段时以 .NET 2.0 为目标。我知道我们将升级到 .NET 3.5,并在时机成熟时删除此类。
The only case where you want to have a class in a namespace that does not belong to you when you are trying to stub something that no longer/yet exists however is used in a code-base you don't want to change.
For example I have written a HashSet implementation and placed in the System.Collections.Generic namespace for an application that was being targeted to .NET 2.0 when .NET 3.5 was in beta. I knew that we will upgrade to .NET 3.5 and removed this class when the time come.
此处似乎表述得很清楚:
“开发类库的设计指南”也有类似的规则:
和:
但是,当然,您 如果你真的想的话可以做到。
It seems pretty clearly stated here:
And the "Design Guidelines for Developing Class Libraries" carry a similar rule:
And:
But, of course, you can do it if you really want to.
建议使用Company.Product.Component 作为命名空间。我认为这可能包括不在您自己的产品中使用框架名称空间。请参阅 MSDN:命名空间的名称(开发类库的设计指南
)。
就我个人而言,我永远不会为我自己的类使用
System
命名空间,就像它们不属于 .NET 框架一样。我也不会在 Java 中使用 java.lang。然而,这只是我个人的意见。希望有帮助。
干杯,马蒂亚斯
There is a recommendation to use
Company.Product.Component
as namespaces. This may include not using Framework namespaces in your own product I think. See MSDN: Names of Namespaces (Design Guidelines for Developing Class Libraries).
Personally I'd never use
System
namespace for my own classes, just as they don't belong to the .NET framework. Neither I'd usejava.lang
in Java. However, this is just my personal opinion.Hope that helps.
Cheers, Matthias