vb.net System namespace conflict with sibling namespace

我的梦境 提交于 2021-01-05 06:51:30

问题


I have a class EventArgs(Of T) in my solution.
This class is located in the namespace Acme.Infrastructure.Interface.
A class with the same name EventArgs exists in the System namespace.

In another project in my solution I have a class Acme.BusinessModules.MyModule.MyClass
When attempting to use the EventArgs class I have to fully qualify the class name or the compiler thinks I am using the System.EventArgs class.

My understanding of namespace resolution was that the compiler would first look for the class in the current namespace, and then its parents. It seems that the compiler checks in System before it checks in sibling namespaces.

Is it correct that System is checked before the sibling? Or is this behaviour caused by other issues (Imports order?)?


回答1:


Look at the namespaces involved:

  • Acme.BusinessModules.MyModule
  • Acme.Infrastructure.Interface

There's no reason why the compiler would look in Acme.Infrastructure.Interface when compiling a class in Acme.BusinessModules.MyModule. Now if the code you were writing were in (say) Acme.Infrastructure.Interface.SomethingElse then that would be a different matter - but here it looks like the compiler is doing absolutely the right thing. The fact that both namespaces start with Acme is neither here nor there.

As you yourself said:

My understanding of namespace resolution was that the compiler would first look for the class in the current namespace, and then its parents.

The ancestor namespaces here are Acme.BusinessModules and Acme - not Acme.Infrastructure.Interface.

I would also strongly advise you not to create class names which conflict with those in System. It's really just asking for trouble.



来源:https://stackoverflow.com/questions/8155976/vb-net-system-namespace-conflict-with-sibling-namespace

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!