C# Project & Namespaces Question
I am creating a little Math library for myself contained within a single project and am running into some issues with namespaces. I have the project MyMathLib
and the top level namespace:
namespace MyMathLib
{ ... }
and in a separate file...
namespace MyMathLib.Addition
{ ... }
and...
namespace MyMathLib.Subtraction
{ ... }
In the MyMathLib.Subtraction
namespace I have a method that needs to use a static method SomeClass.Work()
defined in MyMathLib.Addition
so I included using MyMathLib.Addition
at the beginning of the Subtraction file. But when I try to use the method it would like me to first qualify it with Addition.SomeClass.Work()
and I want to be able to just type SomeClass.Work()
. What am I doing wrong?
Thanks!
EDIT
Thanks for the suggestions! In each file, I actually named the class after the namespace (i.e. in the namespace MyMathLib.Addition
is a static class Addition
and in MyMathLib.Subtraction
there is a static class Subtraction
). Apparently this is what caused the issue (looking back, I should have stated this instead of using SomeClass
). If I change the namespace to MyMathLib.MyAddition
while keeping the static class as Addition
, the using MyMathLib.MyAddition
works as I want; that is, I can now just type Addition.Work()
in my static Subtraction
class. I've seen classes named the same as it's containing namespace before, could someone maybe explain why this is causing an issue? Shouldn't the compiler be able to de开发者_运维问答termine whether I want to use the namespace or the class from the context of the code?
I'm guessing that you either have two classes called SomeClass
that are both in namespaces you reference, or you have a variable or property named SomeClass
. Either of these situations would make it impossible for the compiler to know that you're trying to call the static MyMathLib.Addition.SomeClass.Work()
method, but the specific solution the compiler is suggesting makes it seem more likely to be the former.
Update
Seeing your edit, that makes sense. If you were using these in a namespace outside of MyMathLib
, then you would still be able to avoid this namespace conflict. However, because you are inside the MyMathLib.Subtraction
namespace, the compiler will implicitly consider any portion of the namespace "above" you to take precedence over class names. In this case, when you say "Addition", the compiler will look for the following items to resolve the name:
- A class explicitly identified by a
using ... = ...
directive. MyMathLib.Subtraction.Addition
namespace.MyMathLib.Addition
namespace.Addition
namespace.- Any classes in the namespaces identified by
using
statements.
In this case, you're hitting #3 before #4, so you should be able to work around it either by renaming the class or namespace, or by using Yahia's suggestion (#1):
using Addition = MyMathLib.Addition.Addition;
Update 2
After looking at the article you linked to, it sounds like the explicit using
statement still won't work. I guess item #1 actually gets evaluated down around item #4 instead. Bummer. You can use an alias to give the class a different name locally:
using Add = MyMathLib.Addition.Addition;
...
var add = new Add();
But the best solution is still probably just to avoid the namespace collision entirely by changing your namespace or class name.
try putting additionally the floowing line into your substraction source
using SomeClass = Addition.SomeClass;
http://msdn.microsoft.com/en-us/library/dfb3cx8s.aspx
http://www.blackwasp.co.uk/NamespaceAliasQualifier.aspx
Sounds like you're in the Subtraction namespace...add this to the top, inside the namespace declaration:
using Addition;
That should do the trick.
精彩评论