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”.
但是...,我的抽象类嵌套接口测试实施嵌套接口测试界面。
我在这里缺少什么?
在没有递归接口实现的情况下,以下内容确实有效:
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();
}
}
看来递归执行的时候出了问题。
你缺少一个通用的约束GetNestedInterface<>()
。改成这样:
public TNestedInterface GetNestedInterface<TNestedInterface>()
where TNestedInterface :
NestedInterfaceTest,
INestedInterfaceTest<TNestedInterface>, // new
new()
{
return GateWay<TNestedInterface>.GetNestedInterface();
}
请注意,第二个约束是新的。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)