'Changes to VB.NET namespace behavior in .NET vs .NET Framework
I'm upgrading VB.NET projects from .NET Framework 4.8 to .NET 6 and I'm very confused by changes to namespace behavior.
I have one project (I'll call it "project A") configured with a root namespace of "Abc.Library.Database" and a class defined like so:
Public Class TypeX
End Class
The definition of TypeX is not explicitly contained with a namespace so its namespace should match the root namespace of the project (i.e. "Abc.Library.Database").
I have a project reference setup from another project (project B) to project A. Project B has a root namespace of "Abc.Library". In project B I'm trying to declare a variable of type TypeX like so:
Namespace Abc.Library.Logging
Public Class Logger
Public Sub DoSomething()
Dim x As Abc.Library.Database.TypeX
End Sub
End Class
End Namespace
That worked fine in .NET Framework 4.8, but in .NET 6 that results in a compile error and the suggestion is to change the definition to:
Dim x As Abc.Library.Abc.Library.Database.TypeX
I tried wrapping the definition of TypeX in an explicit namespace as shown below but it made no difference:
Namespace Abc.Library.Database
Public Class TypeX
End Class
End Namespace
I tried searching for an explanation to why and how the behavior of VB.NET namespaces was changed from .NET Framework to .NET 6 (and presumably .NET 5 and .NET Core) but haven't had any luck.
I also found that auto-generated code (e.g. DataSet.Designer.vb) is now wrapping the generated class definition in a namespace that matches the root namespace of the project (which would be fine) but then appends the name of any folders. Searching leads me to believe that there is no way to prevent that from happening.
I'm just trying to figure out how to define my types such that I can keep the declarations of those types the same and avoid the redundant namespace prefixes. Any suggestions would be greatly appreciated.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
