如何从嵌套接口派生类?

时间:2014-01-16 20:43:56

标签: c#

以下代码无法编译:

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args) { }
    }

    class Class : Class.Interface
    {
        internal interface Interface
        {
        }
    }
}

错误消息是:

  

错误CS0146:涉及'ConsoleApplication1.Class'和'ConsoleApplication1.Class.Interface'的循环基类依赖

不明白这一点。

更新: 这可能更具“激励性”( - ;

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args) { }
    }


    class Container : Container.Interface
    {
        // Everything, that is of type "Container.Interface" can be used as child here. 
        // ... including the container itself. 
        Interface[] _children;

        // Is nested to keep the naming consistent. 
        internal interface Interface
        {}
    }
}

Wenn我将接口放在类“Container”之外,它应该被命名为“ContainerChildInterface”之类的东西。在我的项目中,我将有几个这样的类,因此有几个接口。我认为,在这种情况下,使用嵌套接口会更好。

0 个答案:

没有答案