类型“TNestedInterface”必须可转换为“INestedInterfaceTest”才能将其用作参数“TNe

本文关键字:参数 TNe INestedInterfaceTest TNestedInterface 可转换 类型 | 更新日期: 2023-09-27 18:34:13

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

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

类型“TNestedInterface”必须可转换为“INestedInterfaceTest”才能将其用作参数“TNe

您缺少对GetNestedInterface<>()的通用约束。 将其更改为:

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

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

NestedInterfaceTest实现了INestedInterfaceTest<NestedInterfaceTest>GetNestedInterface中对TNestedInterface的要求是它也应该实现INestedInterfaceTest<TNestedInterface>

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

//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()

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

是不可能的
我想

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

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

这做得很好,它可以防止界面"泄漏"。