命名空间和类具有相同的名称? [英] Namespace and class with the same name?

查看:513
本文介绍了命名空间和类具有相同的名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我组织库项目,我有一个中央管理类名为场景图和一大堆生活在场景图的命名空间的其他类。



我真的很想对场景图是 MyLib.Scenegraph 和其它类是 MyLib.Scenegraph。* ,但似乎这样做将是使中的所有其他类的内部类场景图的唯一途径Scenegraph.cs文件和实在太笨重。



相反,我组织为 Mylib.Scenegraph.Scenegraph MyLib.Scenegraph。* ,我发现Visual Studio的哪种类型的作品,但在一定条件下被困惑,我是否指的是类或命名空间。



有举办这个包所以它的方便了用户,而无需在一个不可维护的混乱一起glomming我的所有代码的好办法?


解决方案

我不建议你喜欢的名字命名空间的类,看到的this




框架设计指南中的第3.4节不要为一个命名空间和在命名空间的类型使用
相同的名字之称。这就是:




 命名空间MyContainers.List 
{
公共类列表{...}
}




这是为什么坏?哦,让我算的方式。



您可以自己到您认为您是指
一件事,但实际上指的是别的东西的情况。假设你
在这种不幸的局面结束了:你正在写Blah.DLL和
进口Foo.DLL和Bar.DLL,其中,不幸的是,有型
叫Foo的:




  // Foo.DLL:
命名空间美孚{公共类Foo {}}

// Bar.DLL:
命名空间酒吧{公共类Foo {}}

// Blah.DLL:
命名空间胡说
使用美孚{
;
用吧;
C类{富富; }
}




编译器给出了一个错误。 富是Foo.Foo和
Bar.Foo之间的暧昧。
无赖。我想我会解决这个问题通过完全限定的名称:




  C类{美孚。富富; } 




这现在给出了模棱两可的错误在$美孚b $ b Foo.Foo是Foo.Foo和Bar.Foo 之间的暧昧。我们仍然不知道
什么第一富指的是,直到我们能够明白这一点,我们
甚至不费心揣摩什么,第二个是指



I'm organizing a library project and I have a central manager class named Scenegraph and a whole bunch of other classes that live in the Scenegraph namespace.

What I'd really like is for the scenegraph to be MyLib.Scenegraph and the other classes to be MyLib.Scenegraph.*, but it seems the only way to do that would be to make all the other classes inner classes of Scenegraph in the Scenegraph.cs file and that's just too unwieldy.

Instead, I've organized it as Mylib.Scenegraph.Scenegraph and MyLib.Scenegraph.*, which sort of works but I find Visual Studio gets confused under some conditions as to whether I am referring to the class or the namespace.

Is there a good way to organize this package so it's convenient for users without glomming all my code together in an unmaintainable mess?

解决方案

I don't recommend you to name a class like its namespace, see this.

The Framework Design Guidelines say in section 3.4 "do not use the same name for a namespace and a type in that namespace". That is:

namespace MyContainers.List 
{ 
    public class List { … } 
}

Why is this badness? Oh, let me count the ways.

You can get yourself into situations where you think you are referring to one thing but in fact are referring to something else. Suppose you end up in this unfortunate situation: you are writing Blah.DLL and importing Foo.DLL and Bar.DLL, which, unfortunately, both have a type called Foo:

// Foo.DLL: 
namespace Foo { public class Foo { } }

// Bar.DLL: 
namespace Bar { public class Foo { } }

// Blah.DLL: 
namespace Blah  
{   
using Foo;   
using Bar;   
class C { Foo foo; } 
}

The compiler gives an error. "Foo" is ambiguous between Foo.Foo and Bar.Foo. Bummer. I guess I’ll fix that by fully qualifying the name:

   class C { Foo.Foo foo; } 

This now gives the ambiguity error "Foo in Foo.Foo is ambiguous between Foo.Foo and Bar.Foo". We still don’t know what the first Foo refers to, and until we can figure that out, we don’t even bother to try to figure out what the second one refers to.

这篇关于命名空间和类具有相同的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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