简体   繁体   English

Visual Basic和命名空间问题

[英]Visual Basic and a Namespace Issue

I was cleaning up a Visual Basic (.NET 2.0) solution. 我正在清理Visual Basic(.NET 2.0)解决方案。 Splitting it into two projects. 将其分为两个项目。 150 classes have the Namespace RebateCalculator at the top of the file. 150个类的文件顶部具有命名空间RebateCalculator。 These files are now sitting in Project with the default namespace RebateCalculator. 这些文件现在位于具有默认名称空间RebateCalculator的Project中。 If I were to insert a Class1.cs file and then namespace declaration that all my other files have - then the fully-qualified class name would be RebateCalculator.RebateCalculator.Class1 如果我要插入一个Class1.cs文件,然后再声明我的所有其他文件都具有的名称空间,则完全合格的类名称将为RebateCalculator.RebateCalculator.Class1

Is there something I can stick on the front of the Namespace declaration to make it absolute? 有什么我可以贴在命名空间声明的前面来使其绝对的吗? instead of repeating itself? 而不是重复自己? I'd rather find this kind of solution versus removing the namespace declaration (in case the files get moved again) 我宁愿找到这种解决方案,也不想删除名称空间声明(以防文件再次移动)

End Goal: to be able to do a search/replace on 'Namespace RebateCalculator' to fix the issue in 150 files. 最终目标:能够在“名称空间RebateCalculator”上进行搜索/替换,以解决150个文件中的问题。

I don't know if this answers your question but just in case... 我不知道这是否能回答您的问题,但以防万一。

Open the Project Properties and click on the Application properties. 打开项目属性,然后单击应用程序属性。 CLEAR the Root Namespace there. 在此处清除根命名空间。 Then your Namespace declaration in each file will be "absolute". 然后,每个文件中的命名空间声明将为“绝对”。

By the way...this is very likely to break your application and could take a while to get everything fixed up. 顺便说一句...这很可能破坏您的应用程序,并且可能需要一段时间才能修复所有问题。 Be sure to backup (or at least be committed to your repo) before starting. 在开始之前,请确保备份(或至少要提交给您的仓库)。

As an aside. 作为旁白。 I think that combining a Project Root Namespace with a declared namespace is confusing. 我认为将项目根命名空间与声明的命名空间结合使用会造成混淆。 I usually do one or the other. 我通常会做一个或另一个。

Seth 赛斯

The duplicate answer you posted alluded to the problem. 您发布的重复答案暗示了该问题。

VB.Net has the concept of a "Root Namespace". VB.Net具有“根命名空间”的概念。 When you add a Namespace statement to the code file, the name given is combined with the root namespace to form the full namespace. 当您在代码文件中添加Namespace语句时,给定的名称与根名称空间结合在一起以形成完整的名称空间。

So, in VB.Net, if your root namespace is MyRoot and then you create a class and surround with a namespace of MyNamespace, then the full class name is MyRoot.MyNamespace.ClassName. 因此,在VB.Net中,如果您的根命名空间是MyRoot,然后创建一个类并用MyNamespace命名空间包围,则完整的类名称是MyRoot.MyNamespace.ClassName。 If you don't use a Namespace statement, then the full class name is MyRoot.ClassName. 如果不使用Namespace语句,则完整的类名称为MyRoot.ClassName。 In other words, the root namespace setting contributes to the name of the namespace. 换句话说,根名称空间设置有助于名称空间的名称。

In C#, this is not the case. 在C#中,情况并非如此。 C# has a "default namespace". C#具有“默认名称空间”。 This is simply a namespace definition that is automatically added to new classes when you create them. 这只是一个名称空间定义,在创建新类时会自动将其添加到新类中。 If you remove the namespace declaration, then the class has no namespace. 如果删除名称空间声明,则该类没有名称空间。 In other words, the default namespace setting does not actually contribute to the name of the namespace. 换句话说,默认名称空间设置实际上并没有贡献名称空间的名称。

In C#, whatever is on the namespace declaration is the namespace used. 在C#中,名称空间声明上的内容均为所使用的名称空间。 The default namespace setting only determines what is inserted by default in the code file. 默认名称空间设置仅确定默认情况下在代码文件中插入的内容。

Take this VB program that has a Root Namespace setting of "TestVBConsole" and no namespace statement in the class: 将此VB程序的“根名称空间”设置为“ TestVBConsole”,并且该类中没有名称空间语句:

Module Module1
    Sub Main()
        Dim tc As New TestClass

        Console.WriteLine(tc.GetType().FullName)

    End Sub
End Module

and this class 和这个班

Public Class TestClass
    Public aField As String = "Default"
End Class

The output is 输出是

TestVBConsole.TestClass

The same program in C# outputs C#输出中的相同程序

TestClass

As to your question, I don't think there is any way to override that behavior. 关于您的问题,我认为没有任何方法可以覆盖该行为。 You just have to remember in C#, to make sure your namespace declarations are correct. 您只需要记住在C#中,以确保您的命名空间声明正确。

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

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