问题
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
System.Text.MyCustom mc = new System.Text.MyCustom();
}
}
}
namespace System.Text
{
public class MyCustom { }
}
How do I do this in VB, while having a root namespace in the application, is this possible?
Update: According to the answers I assume there ain't no way to do this. I posted a feature suggestion to Microsoft Connect: Please vote.
回答1:
In VB.NET of VS 2012 this old problem is fixed. Beginning with this version you can escape the root namespace with a leading global
. The following code is buggy in VS 2010 and correct in VS 2012:
Imports Tools
Module Module1
Sub Main()
SayHello()
End Sub
End Module
Namespace Global.Tools
Module TestModule
Sub SayHello()
Console.Out.WriteLine("Hello")
End Sub
End Module
End Namespace
回答2:
I think that the sad truth is that you can't. The namespaces are appended to the root namespace. The documentation gives no hint of any escaping mechanisms. There is a note about using the Global
keyword in relation to namespaces, but I interpret that part of the text as dicussing how to refer to namespaces rather than how to declare them.
回答3:
Take a look this question: Possible to override VB.NET root namespace?.
The bottom line is that your only option is to leave the default namespace empty in the project properties and then wrap all of your class/module definitions in Namespace statements.
回答4:
I the Project properties of a VB project you can change the root namespace. This is per default same as the project name, but you can remove it, and then you have full power of the namespace structure in code. The drawback is that you have to specify the project name as namespace everywhere in the code where you need it...
For C# projects, the similar setting in th Project properties, is only the Default namespace, which is overridden if you specify namespace in the code. For VB projects it specifies the Top level namespace, not the default....
回答5:
Setting a namespace in VB.NET is pretty much the same as declaring a namespace in C#, just with VB.NET syntax! Unfortunately the root namespace is always present so any new namespaces declared will be inside the root namespace.
Namespace ConsoleApplication1
Class Program
Private Shared Sub Main(ByVal args As String())
Dim mc As New System.Text.MyCustom()
End Sub
End Class
End Namespace
Namespace System.Text
Public Class MyCustom
End Class
End Namespace
The above code will give you the following if the root namespace is Test
.
Test.ConsoleApplication1
Test.System.Text
Cheers for the comments guys, was posting on memory!
来源:https://stackoverflow.com/questions/1311099/is-there-a-way-to-escape-root-namespace-in-vb