简体   繁体   English

不能像其他任何元素一样引用堆栈名称空间吗?

[英]Stacking namespaces can't be referenced just like any other element?

So here is a snippet I made where one namespace in inside another (B is inside A). 所以这是我制作的一个片段,其中一个命名空间位于另一个内部(B位于A内部)。 Usually when you use 'using SpaceA;' 通常当您使用“使用SpaceA;”时 you can access all elements without typing SpaceA. 您无需输入SpaceA就可以访问所有元素。 This is the case for class A, but I cannot see SpaceB without having to type SpaceA.SpaceB. 类A就是这种情况,但是我必须输入SpaceA.SpaceB才能看到SpaceB。 Why is this? 为什么是这样? Here is the code: 这是代码:

using System;

using SpaceA;

namespace SpaceA
{
    class A
    {
        public A()
        {
            Console.WriteLine("A");
        }
    }

    namespace SpaceB
    {
        public class B
        {
            public B()
            {
                Console.WriteLine("B");
            }
        }
    }
}

namespace TestingCSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            //This does not work
            SpaceB.B x = new SpaceB.B();

            //This does work
            SpaceA.SpaceB.B y = new SpaceA.SpaceB.B();
        }
    }
}

Usually when you use 'using SpaceA;' 通常当您使用“使用SpaceA;”时 you can access all elements without typing SpaceA. 您无需输入SpaceA就可以访问所有元素。

Only the direct types which are members of SpaceA . 仅属于SpaceA成员的直接类型。 A namespace-or-type-name is never resolved using a namespace in a normal using directive and then another "subnamespace" in the name. namespace-or-type-name永远不会在常规using指令中使用名称空间,然后在名称中using另一个“子名称空间”进行解析。 Note that this has nothing to do with how the types are declared (in terms of having a nested namespace declaration or just namespace SpaceA.SpaceB ) - another example would be: 请注意,这与类型的声明方式无关(就具有嵌套的名称空间声明还是仅具有名称namespace SpaceA.SpaceB )-另一个示例将是:

using System;

...
Xml.Linq.XElement x = null; // Invalid

See section 3.8 of the C# 5 specification for the precise details of how names are resolved. 有关如何解析名称的确切详细信息,请参见C#5规范的3.8节。

One slight difference to this is if you have a using alias directive for a namespace, at which point that can be used to look up other namespaces 与此稍有不同的是,如果您对某个名称空间使用了using别名指令, 可以在该位置使用它来查找其他名称空间

using X = System.Xml;
...
X.Linq.XElement x = null; // Valid

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何以编程方式创建VS 2008 Windows Forms项目,该项目可以像手动创建的任何其他项目一样进行维护 - How to create a VS 2008 Windows Forms project programmatically, that can be maintained just like any other project created manually MVC5将脚手架用于其他名称空间中的类(引用的DLL) - MVC5 using scaffolding for classes from other namespaces (referenced DLL) Blazor 无法从其他文件夹中找到引用的组件 - Blazor can't find referenced component from other folder 可以在其他命名空间中访问内部类吗? - Can internal classes be accessed within other namespaces? 为什么ASP.NET无法加载程序集? 看起来它正在改变被引用的版本 - Why can't ASP.NET load the assembly? It looks like it's changing the version that's being referenced App class 中的公共变量无法在其他 class xamarin.forms 中引用 - public variable in App class can't get referenced in other class xamarin forms 无法引用超链接 EventArgs - Hyperlink EventArgs Can't Be Referenced 从Type读取引用的名称空间 - Read referenced namespaces from Type 如何像ToolTip一样为所有其他类分配函数或属性 - How can I assign a function or property to all of other classes just like ToolTip 有没有像C#中的Bundle一样的东西? - Is there any thing just like Bundle in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM