3
public interface INestedInterfaceTest<TChildType>
    where TChildType : INestedInterfaceTest<TChildType>
{
     List<TChildType> children { get; set; }
}

public abstract class NestedInterfaceTest : INestedInterfaceTest<NestedInterfaceTest>
{
    public List<NestedInterfaceTest> children { get; set; }

    public TNestedInterface GetNestedInterface<TNestedInterface>()
        where TNestedInterface : NestedInterfaceTest, new()
    {
        return GateWay<TNestedInterface>.GetNestedInterface();
    }
}

public class GateWay<TNestedInterface>
    where TNestedInterface : class, INestedInterfaceTest<TNestedInterface>, new()
{
    public static TNestedInterface GetNestedInterface()
    {
        return new TNestedInterface();
    }
}

抽象类中的 GetNestedInterface 方法出现问题。错误消息是:类型 'TNestedInterface' 必须可转换为 'INestedInterfaceTest' 才能在通用类 'GateWay' 中用作参数 'TNestedInterface'

但是...,我的抽象类NestedInterfaceTest实现了InestedInterfaceTest接口。我在这里想念什么?

以下确实有效,没有递归接口实现:

public interface INestedInterfaceTest
{
}

public abstract class NestedInterfaceTest : INestedInterfaceTest
{
    public List<NestedInterfaceTest> children { get; set; }

    public TNestedInterface GetNestedInterface<TNestedInterface>()
        where TNestedInterface : NestedInterfaceTest, new()
    {
        return GateWay<TNestedInterface>.GetNestedInterface();
    }
}

public class GateWay<TNestedInterface>
    where TNestedInterface : class, INestedInterfaceTest, new()
{
    public static TNestedInterface GetNestedInterface()
    {
        return new TNestedInterface();
    }
}

似乎在递归实现中出错了。

4

3 回答 3

1

您缺少对GetNestedInterface<>(). 将其更改为:

public TNestedInterface GetNestedInterface<TNestedInterface>()
    where TNestedInterface : 
        NestedInterfaceTest, 
        INestedInterfaceTest<TNestedInterface>, // new
        new()
{
    return GateWay<TNestedInterface>.GetNestedInterface();
}

请注意,第二个约束是新的。

于 2013-10-07T20:52:42.023 回答
0

NestedInterfaceTest实施INestedInterfaceTest<NestedInterfaceTest>,其中的要求TNestedInterfaceGetNestedInterface它也应该实施INestedInterfaceTest<TNestedInterface>

如果我创建一个NestedInterfaceTest不重新实现INestedInterfaceTest<>接口的子类,则不会满足该约束:

//does not implement INestedInterfaceTest<Derived>
class Derived : NestedInterfaceTest {}
class Derived2 : NestedInterfaceTest {}

Derived d = new Derived();
// breaks because GateWay<Derived2> cannot be constructed
d.GetNestedInterface<Derived2>();

我看到的最简单的解决方法可能不适合您的对象模型是重新定义对GateWay类的约束:

public class GateWay<TNestedInterface>
  where TNestedInterface : class, INestedInterfaceTest<NestedInterfaceTest>, new()

这样,任何从 NestedInterfaceTest 继承的类都将满足约束,而无需重新实现接口,因为 NestedInterfaceTest 满足了它。这样做可能更方便:

public class GateWay<TNestedInterface>
  where TNestedInterface : class, NestedInterfaceTest, new()

无论如何,即使你设法让它工作,对于类的消费者和(最终的)维护者来说,它仍然令人困惑。此外,您几乎永远无法确定某些误用是不可能的

于 2013-10-07T20:58:54.980 回答
0

我想我解决了我自己的问题。请查看代码并让我知道您的想法:

public interface INestedInterfaceTest<TChildType>
    where TChildType : INestedInterfaceTest<TChildType>
{
    List<TChildType> children { get; set; }
}

public abstract class NestedInterfaceTest<TChildNestedInterface> : INestedInterfaceTest<TChildNestedInterface>
    where TChildNestedInterface : INestedInterfaceTest<TChildNestedInterface>
{
    public List<TChildNestedInterface> children { get; set; }

    public virtual TNestedInterface GetNestedInterface<TNestedInterface>()
        where TNestedInterface : NestedInterfaceTest<TChildNestedInterface>, INestedInterfaceTest<TNestedInterface>, new()
    {
        return GateWay<TNestedInterface>.GetNestedInterface();
    }
}

public class GateWay<TNestedInterface>
    where TNestedInterface : class, INestedInterfaceTest<TNestedInterface>, new()
{
    public static TNestedInterface GetNestedInterface()
    {
        List<TNestedInterface> nestedChildren = new List<TNestedInterface>();
        return new TNestedInterface
            {
                children = nestedChildren
            };
    }
}

public class NestedClass : NestedInterfaceTest<NestedClass>
{
    public NestedClass GetNestedClass()
    {
        return GetNestedInterface<NestedClass>();
    }
}

那确实成功了,它可以防止界面“泄漏”。

于 2013-10-07T21:33:33.547 回答