[英]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
. 它的命名空间是MyCompanyName.ProductName.FunctionSet
。 At this time, this project includes two classes; 目前,该项目包括两个班级。 customer.cs
and core.cs
. customer.cs
和core.cs
The core class is static and contains a static method EntryPoint(). 核心类是静态的,并且包含静态方法EntryPoint()。
I have a console app in the solution. 我在解决方案中有一个控制台应用程序。 Its namespace is MyCompanyName.ProductName.TaskMan
. 它的命名空间是MyCompanyName.ProductName.TaskMan
。 I created a reference to the core project above and added a using directive for the namespace. 我创建了对上面核心项目的引用,并为名称空间添加了using指令。
One of the lines in Main()
is Core.EntryPoint();
Main()
中的其中Core.EntryPoint();
是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();
现在,为了解决错误(无法解决符号'EntryPoint'),我需要将行更改为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: 我怀疑其中之一是正确的:
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. 尽管名称空间是相对于当前名称空间解析的,但名称空间相对于using
指令并没有解析。 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. 有关完整的细节,请参阅C#规范的3.8节。
Have you tried adding the function in a using statement at the beginning of the file? 您是否尝试过在文件开头的using语句中添加功能?
using FunctionSet.Core.EntryPoint
EntryPoint();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.