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();
}
}
似乎在递归实现中出错了。