【问题标题】:Class Library Namespace Hierarchy Not Working类库命名空间层次结构不起作用
【发布时间】:2016-03-09 05:15:09
【问题描述】:

我正在尝试在我的类库中实现 C# 中使用的命名空间层次结构。这是我想要做的:

namespace Parent
{
    namespace Child
    {
        Class ChildClass {  }
    }

    Class ParentClass {  }
}

编译类库后没有按预期工作。这是我预期的工作原理。

要访问ChildClass,必须using Parent.Child。但是可以通过using Parent 访问ParentClass

我可以在不编译类库的情况下执行此操作,但将 cs 文件添加到项目中。但是当我编译为 DLL 并将其作为引用添加到项目中时,我无法访问子命名空间。

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

有没有办法在 C# 中实现这一点?

【问题讨论】:

    标签: c# dll namespaces class-library


    【解决方案1】:

    我认为您的课程缺少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{ }
    }
    

    ChildClass.cs

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

    这似乎对我有用。

    【讨论】:

    • 你在编译完类库后试过这个吗?
    • 我有多个文件。这似乎是问题所在。你能解释一下为什么吗?
    • 每个类在单独的文件中?
    • 分班似乎也不错。
    • 你能把你的示例项目发给我吗?电子邮件:noor.xbyte@gmail.com
    【解决方案2】:

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

    阅读以下内容:Parent child class relationship design pattern

    这应该会让你朝着正确的方向开始:

    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;
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多