Visual Basic和命名空间问题 [英] Visual Basic and a Namespace Issue

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

问题描述

我在收拾一个Visual Basic(.NET 2.0)的解决方案。分裂成两个项目。 150班有命名空间RebateCalculator在文件的顶部。 这些文件现在坐在项目的默认命名空间RebateCalculator。如果我要插入一个Class文件,然后命名空间声明,我的所有其他文件都 - 那么完全合格的类名是RebateCalculator.RebateCalculator.Class1

I was cleaning up a Visual Basic (.NET 2.0) solution. Splitting it into two projects. 150 classes have the Namespace RebateCalculator at the top of the file. These files are now sitting in Project with the default namespace RebateCalculator. 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

有什么我可以在命名空间声明的前坚持,使其绝对?而不是重演?我宁愿发现这种解决方案对去除命名空间声明(如果该文件被再次移动)

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)

最终目标:能够做一个搜索/替换命名空间RebateCalculator在150个文件解决问题。

End Goal: to be able to do a search/replace on 'Namespace RebateCalculator' to fix the issue in 150 files.

推荐答案

您贴提到的问题重复的答案。

The duplicate answer you posted alluded to the problem.

VB.Net有一个根命名空间的概念。当您添加一个命名空间语句将code文件,给出的结合根命名空间的名称,形成了完整的命名空间。

VB.Net has the concept of a "Root Namespace". When you add a Namespace statement to the code file, the name given is combined with the root namespace to form the full namespace.

所以,在VB.Net,如果你的根命名空间是MyRoot,然后创建一个类,并围绕与myNameSpace对象的命名空间,那么完整的类名是MyRoot.MyNamespace.ClassName。如果不使用命名空间语句,则完整的类名是MyRoot.ClassName。换句话说,根名称空间设置有助于命名空间的名称

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. If you don't use a Namespace statement, then the full class name is MyRoot.ClassName. In other words, the root namespace setting contributes to the name of the namespace.

在C#中,不是这种情况。 C#有一个默认命名空间。这是一个简单的自动添加到新的类时,你创建它们命名空间的定义。如果您删除的命名空间声明,那么类没有命名空间。换句话说,默认名称空间设置实际上并不向命名空间的名称

In C#, this is not the case. C# has a "default namespace". 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.

在C#中,无论是在名称空间声明是所使用的命名空间。默认的命名空间设置只决定了默认情况下,在code文件插入。

In C#, whatever is on the namespace declaration is the namespace used. The default namespace setting only determines what is inserted by default in the code file.

取本VB程序,有TestVBConsole的根命名空间设置,并在课堂上没有命名空间的语句:

Take this VB program that has a Root Namespace setting of "TestVBConsole" and no namespace statement in the class:

Module Module1
    Sub Main()
        Dim tc As New TestClass

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

    End Sub
End Module

和这个类

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

输出为

TestVBConsole.TestClass

在C#中输出相同的程序

The same program in C# outputs

TestClass

至于你的问题,我不认为有什么办法可以重写该行为。你只需要记住在C#中,以确保您的命名空间的声明是正确的。

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.

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

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