Why do I need to include part of my namespace despite having a 'using' declaration?
I have one project in my solution geared towards providing some core functionality. It's namespace is MyCompanyName.ProductName.FunctionSet
. At this time, this project includes two classes; customer.cs
and core.c开发者_StackOverflow中文版s
. The core class is static and contains a static method EntryPoint().
I have a console app in the solution. Its namespace is MyCompanyName.ProductName.TaskMan
. I created a reference to the core project above and added a using directive for the namespace.
One of the lines in Main()
is Core.EntryPoint();
. Note, it does not include any part of the namespace. The code was running fine.
Now, in order to resolve an error (Cannot resolve symbol 'EntryPoint') I needed to change the line to FunctionSet.Core.EntryPoint();
. I needed to add the last part of the namespace.
Why did I need to add that portion of the namespace? Any ideas why it suddenly stopped working? Is there anything I can do to omit the namespace portion? It's not a big deal if I'm stuck with this but I do want to know the how and why.
EDIT: I've preserved the bulky bit below, but I'm now not sure it's relevant.
I suspect that one of these is true:
- Your namespaces aren't quite what you think they are (in particular for Core)
- You don't have the using directive you think you do
If you could post a short but complete program (just the two classes should be enough, and just the namespace, using directives, class declaration and methods - no need for any business logic) it should be easy enough to sort out.
Namespaces aren't resolved relative to using
directives, although they are resolved relative to the current namespace. For example:
namespace Outer.Inner
{
class Foo
{
public static void Bar() {}
}
}
namespace Outer.OtherInner
{
class Test1
{
static void Method()
{
// Resolved to Outer.Inner.Foo.Bar()
Inner.Foo.Bar();
}
}
}
namespace OtherOuter
{
using Outer;
class Test2
{
static void Main()
{
// This is invalid
Inner.Foo.Bar();
}
}
}
For the full gory details, look at section 3.8 of the C# specification.
Have you tried adding the function in a using statement at the beginning of the file?
using FunctionSet.Core.EntryPoint
EntryPoint();
精彩评论