C# 类库命名空间层次结构不工作

C# 类库命名空间层次结构不工作,c#,dll,namespaces,class-library,C#,Dll,Namespaces,Class Library,我试图在类库中实现C#中使用的名称空间层次结构。以下是我试图做的: namespace Parent { namespace Child { Class ChildClass { } } Class ParentClass { } } 编译类库后,它没有按预期工作。这是我预期的工作原理 要访问ChildClass,必须使用Parent.Child。但是,只需使用Parent,就可以访问ParentClass 我可以不用编译类库,而是将cs文

我试图在类库中实现C#中使用的名称空间层次结构。以下是我试图做的:

namespace Parent
{
    namespace Child
    {
        Class ChildClass {  }
    }

    Class ParentClass {  }
}
编译类库后,它没有按预期工作。这是我预期的工作原理

要访问
ChildClass
,必须使用Parent.Child
。但是,只需使用Parent
,就可以访问
ParentClass

我可以不用编译类库,而是将cs文件添加到项目中。但是,当我编译为DLL并将其添加为项目中的引用时,我无法访问子名称空间

更新:每个类我有不同的文件。当我将所有名称空间和类写入一个文件时,它似乎可以工作。但是为什么呢


在C#中是否有实现这一点的方法?

我认为您的类缺少
public
;下面的代码适合我

namespace Parent
{
    namespace Child
    {
        public class ChildClass { }
    }
    public class ParentClass
    {
    }
}
我可以创造

Parent.ParentClass p;
Parent.Child.ChildClass c;
这是你期望的工作原理

编辑:每个班级方法的单独cs文件

ParentClass.cs

namespace Parent
{
    public class ParentClass{ }
}
namespace Parent
{
    namespace Child
    {
        public class ChildClass { }
    }
}
ChildClass.cs

namespace Parent
{
    public class ParentClass{ }
}
namespace Parent
{
    namespace Child
    {
        public class ChildClass { }
    }
}

这似乎对我有用。

我认为你的课程缺少
public
;下面的代码适合我

namespace Parent
{
    namespace Child
    {
        public class ChildClass { }
    }
    public class ParentClass
    {
    }
}
我可以创造

Parent.ParentClass p;
Parent.Child.ChildClass c;
这是你期望的工作原理

编辑:每个班级方法的单独cs文件

ParentClass.cs

namespace Parent
{
    public class ParentClass{ }
}
namespace Parent
{
    namespace Child
    {
        public class ChildClass { }
    }
}
ChildClass.cs

namespace Parent
{
    public class ParentClass{ }
}
namespace Parent
{
    namespace Child
    {
        public class ChildClass { }
    }
}

这似乎对我有用。

您正在嵌套类和名称空间,这一切似乎有点混乱。为什么不保持一个更平坦的名称空间结构并在类中嵌套呢。请记住,不需要嵌套名称空间或类来维护父子关系

阅读以下内容:

这将使您从正确的方向开始:

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication2
{
    using System;
    using System.Collections.Generic;

    public class ChildClass
    {
        private ParentClass parent;

        public ChildClass(ParentClass parentIn)
        {
            parent = parentIn;
        }

        public ParentClass Parent
        {
            get { return parent; }
        }
    }

    public class ParentClass
    {
        private List<ChildClass> children;

        public ParentClass()
        {
            children = new List<ChildClass>();
        }

        public ChildClass AddChild()
        {
            var newChild = new ChildClass(this);
            children.Add(newChild);
            return newChild;
        }
    }


    public class Program
    {
        public static void Main()
        {
            Console.WriteLine("Hello World");

            var p = new ParentClass();
            var firstChild = p.AddChild();
            var anotherChild = p.AddChild();
            var firstChildParent = firstChild.Parent;
            var anotherChildParent = anotherChild.Parent;
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统文本;
命名空间控制台应用程序2
{
使用制度;
使用System.Collections.Generic;
公营儿童班
{
私人家长阶级家长;
公共子类(ParentClass parentIn)
{
parent=parentIn;
}
公共父类父类
{
获取{return parent;}
}
}
公共类父类
{
私人名单儿童;
公共父类()
{
children=新列表();
}
公共子类AddChild()
{
var newChild=新的子类(此);
添加(newChild);
返回新孩子;
}
}
公共课程
{
公共静态void Main()
{
Console.WriteLine(“你好世界”);
var p=新的父类();
var firstChild=p.AddChild();
var anotherChild=p.AddChild();
var firstChildParent=firstChild.Parent;
var anotherChildParent=anotherChild.Parent;
}
}
}

您正在嵌套类和名称空间,这一切似乎有点混乱。为什么不保持一个更平坦的名称空间结构并在类中嵌套呢。请记住,不需要嵌套名称空间或类来维护父子关系

阅读以下内容:

这将使您从正确的方向开始:

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication2
{
    using System;
    using System.Collections.Generic;

    public class ChildClass
    {
        private ParentClass parent;

        public ChildClass(ParentClass parentIn)
        {
            parent = parentIn;
        }

        public ParentClass Parent
        {
            get { return parent; }
        }
    }

    public class ParentClass
    {
        private List<ChildClass> children;

        public ParentClass()
        {
            children = new List<ChildClass>();
        }

        public ChildClass AddChild()
        {
            var newChild = new ChildClass(this);
            children.Add(newChild);
            return newChild;
        }
    }


    public class Program
    {
        public static void Main()
        {
            Console.WriteLine("Hello World");

            var p = new ParentClass();
            var firstChild = p.AddChild();
            var anotherChild = p.AddChild();
            var firstChildParent = firstChild.Parent;
            var anotherChildParent = anotherChild.Parent;
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统文本;
命名空间控制台应用程序2
{
使用制度;
使用System.Collections.Generic;
公营儿童班
{
私人家长阶级家长;
公共子类(ParentClass parentIn)
{
parent=parentIn;
}
公共父类父类
{
获取{return parent;}
}
}
公共类父类
{
私人名单儿童;
公共父类()
{
children=新列表();
}
公共子类AddChild()
{
var newChild=新的子类(此);
添加(newChild);
返回新孩子;
}
}
公共课程
{
公共静态void Main()
{
Console.WriteLine(“你好世界”);
var p=新的父类();
var firstChild=p.AddChild();
var anotherChild=p.AddChild();
var firstChildParent=firstChild.Parent;
var anotherChildParent=anotherChild.Parent;
}
}
}

编译类库后是否尝试过此操作?我有多个文件。这似乎就是问题所在。你能解释一下为什么吗?每门课都在单独的文件中?单独的课似乎也可以。你能把你的样本项目发给我吗。电子邮件:noor。xbyte@gmail.comDid你在编译类库后试过这个吗?我有多个文件。这似乎就是问题所在。你能解释一下为什么吗?每门课都在单独的文件中?单独的课似乎也可以。你能把你的样本项目发给我吗。电子邮件:noor。xbyte@gmail.com