关于显式(命名)与隐式(全局或根)命名空间的 VB.NET 命名空间问题
我有一个解决方案,其中包含许多使用相同根命名空间的项目。没有代码文件明确命名命名空间。假设根命名空间是ExampleRootNamespace
。
现在,当我想将显式命名的名称空间添加到我正在处理的代码文件之一时,问题就出现了。我希望能够将此代码与程序集的其余部分隔离,以便能够对其运行 FxCop。因此,我将诸如 Namespace Interfaces.CSV
之类的内容添加到代码文件中。
这会导致引用此程序集的任何代码都需要显示 Imports ExampleRootNamespace.Interfaces.CSV
。到目前为止,一切都很好。我什至可以针对程序集运行 FxCop。现在的问题是,在其他程序集中我不能再说这样的话:
Public class frmInputBoolean Inherits
ExampleRootNameSpace.frmFormTemplate
Visual Studio 现在要求我将命名空间重命名为:
Public class frmInputBoolean Inherits
Global.ExampleRootNameSpace.frmFormTemplate
有数百个与此相关的错误。所以我的问题是:
1)为什么第一次在根目录下命名命名空间会导致程序出现问题?
2)是否有不重命名的情况下解决此问题的方法?
我还想补充一点,关于 ExampleRootNamespace.Interfaces.CSV
我没有在代码库中的任何地方引用它。我目前只是从单元测试项目中引用它。所以我不明白为什么添加这个命名空间会导致问题。
I have a solution that contains many projects all using the same root namespace. No code files explicitly name a namespace. So lets say the root namespace is ExampleRootNamespace
.
Now a problem comes into play when I want to add an explicitly named namespace to one of the code files I am working on. I want to be able to isolate this code from the rest of the assembly to be able to run FxCop against it. So I add something like Namespace Interfaces.CSV
to the code file.
This causes any code that references this assembly to need to say Imports ExampleRootNamespace.Interfaces.CSV
. So far so good. I can even run FxCop against the assembly. The problem now is that in other assemblies I cannot say any longer things like:
Public class frmInputBoolean Inherits
ExampleRootNameSpace.frmFormTemplate
Visual Studio is now asking me to rename the namespace to:
Public class frmInputBoolean Inherits
Global.ExampleRootNameSpace.frmFormTemplate
There are hundreds of errors related to this. So my questions are:
1) Why would basically naming a namespace under the root for the first time cause issues with the program?
2) Are there any workarounds to this issue without renaming?
I also want to add that with regards to ExampleRootNamespace.Interfaces.CSV
I am not referencing this anywhere in the codebase. I'm currently just referencing it from a unit test project. So I don't see why adding this namespace causes an issue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 C# 中,尝试在您的命名空间中使用
USING
运算符。关于那些具有全球性问题的问题。
遗憾的是,我不认为 VB.NET 中存在简单的解决方案
In C# try utilizing the
USING
operator with your namespace.On the ones that have the global issue.
sadly, I do not believe an easy solution exists for you in VB.NET
嗯,看来这可能是 Visual Studio 2008 中的一个错误。因为代码没有更改,但所需的
Global
前缀的问题不再存在。我这么说是因为我检查了一个有问题的代码文件并尝试添加(正如 Meakins 建议的那样):
当我这样做时,发生了两件事。
1) 错误突出显示和错误更正建议已被删除,ExampleRootNamespace 被 Visual Studio 识别。现在代码也可以编译了。
2) 由于在导入语句中使用了 Global,
Imports ExampleRootNamespace = Global.ExampleRootNamespace
无效。 Visual studio 说:“在这种情况下不允许‘全局’;需要标识符。”这意味着这行代码将无法编译。所以我把它全部删除了。奇怪的是,尽管它不存在(因此代码返回到以前的状态),但没有更多错误。我还重新启动了 Visual Studio(但在执行上述操作之后)。如果你问我的话,这很奇怪!
Well, it appears this may be a bug in Visual Studio 2008. As the code has not changed but the problem with the required
Global
prefix is no longer there.I say this because I checked out one of the offending code files and tried to add (as Meakins suggested):
When I did this two things happened.
1) The error highlighting and error correction suggestions were removed and ExampleRootNamespace was recognized by Visual Studio. Also the code now compiles.
2)
Imports ExampleRootNamespace = Global.ExampleRootNamespace
is not valid because of the use of Global in the imports statement. Visual studio says: "'Global' not allowed in this context; identifier expected." This Means that this line of code will not compile. So I removed it all together. Oddly enough despite it not being there (and thus the code returning to as before) there are no more errors.I also restarted visual studio (but after doing the above). This is quite odd if you ask me!