.NET 单个类的多个命名空间

.NET Multiple Namespaces for single class(.NET 单个类的多个命名空间)

本文介绍了.NET 单个类的多个命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以让一个类驻留在两个命名空间中,我该怎么做?

Is it possible to have a single class reside within two name-spaces and how can I do this?

澄清一下:我们有一个类库(假设根命名空间是 classLib1),它随着时间的推移而增长(更多类),我想在逻辑上将类分组到不同的命名空间中.然而,一些较旧的类需要被分组到这些新的命名空间中(例如 classLib1.section1),这样做会破坏使用这个类库的其他程序集中的遗留代码.所以我希望能够使用两个名称空间来引用一个类,直到我们可以逐步淘汰旧的.

To clarify: We have a class library (let say root namespace is classLib1), which has grown over time (more classes) and I want to logically group classes into different namespaces. However some of the older classes need to be grouped into these new namespaces (e.g classLib1.section1) and doing so will break legacy code in other assemblys that use this class library. So I want to be able to refer to a class using both name-spaces until we can phase the old ones out.

我找不到任何关于此的信息,这表明人们不想这样做是有原因的!?!

I can't find any information on this, which suggests there is a reason that people would not want to do this!?!

推荐答案

不行,没有办法给单个类起两个名字(命名空间其实只是类名的一部分).

No, there is no way to give a single class two names (the namespace is actually just a part of the class name).

作为一种解决方法,您可以将类移动到它们的新位置,并在旧位置围绕它们创建薄包装(外观模式).当然,更好的解决方案是移动类并相应地修复遗留代码.

As a workaround, you could move the classes to their new location, and create thin wrappers around them at the old location (Facade Pattern). A better solution, of course, would be to move the classes and fix the legacy code accordingly.

这篇关于.NET 单个类的多个命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:.NET 单个类的多个命名空间

基础教程推荐