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

2023-09-03 03:18:28 作者:声音吃人

是否有可能有一个单独的类驻留两名-空间中,我该怎么办呢?

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

要澄清:我们有一个类库(让说根命名空间是classLib1),它在持续增长(更多的类),我想逻辑分组类到不同的命名空间。 然而,一些上了年纪的类需要分组到这些新的命名空间(如classLib1.section1),而且这样做会打破传统的code在使用这个类库等大会的。所以,我希望能够用这两个名称空间是指一类,直到我们能淘汰旧的了。

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

作为一种解决方法,你可以移动类到新的位置,并在原来的位置(外观模式)创建他们周围薄包装。当然,更好的解决方案,将移动类和相应的解决遗留code。

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.