简体   繁体   English

C#中的嵌套命名空间

[英]Nested Namespaces in C#

What does this answer to another question mean? 是什么这样的回答另一个问题是什么意思?

Yes, creating a nested namespace is possible. 是的,可以创建一个嵌套的名称空间。 However, I believe the preferred methodology for something like this would be to create a resource file 但是,我认为类似的首选方法是创建资源文件

This gives you the added benefit of being able to change without a code change, as well as to support multiple languages. 这为您带来了额外的好处,即无需更改代码即可进行更改以及支持多种语言。

I am making a few namespaces and I want them to be all under one namespace ( MyNamespace ), but I don't feel like going and changing all the code to add nested interfaces, and naming them MyNamespace.N1 is not very compatible with intelli-sense. 我正在创建一些名称空间,但我希望它们全部位于一个名称空间( MyNamespace )下,但是我不想去更改所有代码以添加嵌套接口,并MyNamespace.N1命名MyNamespace.N1MyNamespace.N1不太兼容-感。

Edit: My main problem is that namespace MyNamespace.N1 { } does not seem to work right with renames and stuff like that. 编辑:我的主要问题是namespace MyNamespace.N1 { }似乎不能正确使用重命名和类似的东西。 Is it even a true nested namespace declaration? 它甚至是一个真正的嵌套名称空间声明吗?

Underlying Question: A namespace declaration with a period in it is two namespaces. 潜在的问题:带有句点的名称空间声明是两个名称空间。 Why, then, when I rename it, does it not act right with the rename. 为什么当我重命名时,它不能正确执行该重命名操作。 My question may be vague, but I have such a problem with it that I thought for sure a lot of other people would have run into it. 我的问题可能含糊不清,但是我有这样一个问题,以至于我可以肯定很多其他人也会遇到它。 If not, then just close it and I'll try to figure it out some other way. 如果不是,则将其关闭,我将尝试以其他方式解决。


I'm trying to figure out how to put all the namespaces in a project under one namespace. 我试图弄清楚如何将一个项目中的所有名称空间放在一个名称空间下。 So you would access them using MyProject.MyN1.MyClass . 因此,您将使用MyProject.MyN1.MyClass访问它们。 If I declare the namespace like namespace MyProject.MyN1 { } and then try to rename it, the intelli-sense only looks at "MyProject" as the namespace, MyN1 is peripheral. 如果我像namespace MyProject.MyN1 { }那样声明命名namespace MyProject.MyN1 { } ,然后尝试重命名它,则智能感知只会将“ MyProject”视为命名空间,而MyN1是外围设备。 But I'm really trying to rename "MyN1" and it doesn't like doing that. 但是我真的想重命名“ MyN1”,它不喜欢这样做。

Well, I tried that, and now I'm not sure what my problem was in the first place. 好吧,我尝试过了,现在我不确定首先要解决的问题是什么。 As I described it it works fine. 正如我描述的那样,它工作正常。 I think it was that if I try to add or remove a period in the namespace declaration it doesn't rename it right. 我认为这是如果尝试在名称空间声明中添加或删除句点,则不会正确重命名。

One way that would probably work is to using a global find and replace. 一种可行的方法是使用全局查找和替换。 What your actually trying to do is to rename two namespaces into one, and it's not going to let you. 您实际要做的是将两个名称空间重命名为一个,这不会让您失望。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM