简体   繁体   English

为什么尽管有“使用”声明,但仍需要包括我的部分名称空间?

[英]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.cscore.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: 我怀疑其中之一是正确的:

  • Your namespaces aren't quite what you think they are (in particular for Core) 您的名称空间并不完全符合您的想法(特别是对于Core)
  • You don't have the using directive you think you do 您没有自己认为的using指令

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.

相关问题 为什么在我的属性声明“[field:NonSerialized]”中需要“field:”? - Why do I need “field:” in my attribute declaration “[field:NonSerialized]”? 在引用枚举类型时,为什么需要在枚举的声明中添加名称空间和类? - in referencing enum type why do i need to add namespace and class in declaration of the enum? 尽管拥有正确的NuGet软件包,为什么我仍需要在计算机上安装MySQL连接器? - Why do I still need the MySQL Connector installed on the computer despite having the proper NuGet packages? 为什么我需要.Include() collections - Why do I need to .Include() collections 为什么我需要为Xpath查询指定命名空间管理器 - Why do I need to specify a Namespace manager for Xpath querys C#系统命名空间-为什么需要导入它? - C# System Namespace - why do I need to import it? 我是否需要在实体框架模型中包括所有字段 - Do I need to include all fields in my entity framework model 为什么在进行序列化时需要指定xml名称空间? - Why do I need to specify an xml namespace when I do serialization? 如何在Mac OS X上的VSCode项目中包含“System.Runtime.Serialization.Json”命名空间? - How do I include 'System.Runtime.Serialization.Json' namespace in my VSCode project on Mac OS X? 在闭包中使用属性名称时,是否需要包括“ this”? - Do I need to include the 'this' when using a property name in a closure?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM