Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 类型';TNestedInterface';必须可转换为';INESTEDInterfactest';为了将其用作参数';TNestedInterface'; 公共接口INestedInterfaceTest 其中TChildType:INestedInterfaceTest { 列出子项{get;set;} } 嵌套interfacetest的公共抽象类:inestedininterfacetest { 公共列表子项{get;set;} 公共TNestedInterface GetNestedInterface() 其中TNestedInterface:NestedInterfaceTest,new() { 返回GateWay.GetNestedInterface(); } } 公共类网关 其中TNestedInterface:class,INestedInterfaceTest,new() { 公共静态TNestedInterface GetNestedInterface() { 返回新的TNestedInterface(); } }_C#_Generics_Recursion_Interface - Fatal编程技术网

C# 类型';TNestedInterface';必须可转换为';INESTEDInterfactest';为了将其用作参数';TNestedInterface'; 公共接口INestedInterfaceTest 其中TChildType:INestedInterfaceTest { 列出子项{get;set;} } 嵌套interfacetest的公共抽象类:inestedininterfacetest { 公共列表子项{get;set;} 公共TNestedInterface GetNestedInterface() 其中TNestedInterface:NestedInterfaceTest,new() { 返回GateWay.GetNestedInterface(); } } 公共类网关 其中TNestedInterface:class,INestedInterfaceTest,new() { 公共静态TNestedInterface GetNestedInterface() { 返回新的TNestedInterface(); } }

C# 类型';TNestedInterface';必须可转换为';INESTEDInterfactest';为了将其用作参数';TNestedInterface'; 公共接口INestedInterfaceTest 其中TChildType:INestedInterfaceTest { 列出子项{get;set;} } 嵌套interfacetest的公共抽象类:inestedininterfacetest { 公共列表子项{get;set;} 公共TNestedInterface GetNestedInterface() 其中TNestedInterface:NestedInterfaceTest,new() { 返回GateWay.GetNestedInterface(); } } 公共类网关 其中TNestedInterface:class,INestedInterfaceTest,new() { 公共静态TNestedInterface GetNestedInterface() { 返回新的TNestedInterface(); } },c#,generics,recursion,interface,C#,Generics,Recursion,Interface,抽象类中的GetNestedInterface方法出错。 错误消息是:类型“TNestedInterface”必须可转换为“INestedInterfaceTest”,才能将其用作泛型类“GateWay”中的参数“TNestedInterface” 但是…,我的抽象类NestedInterfaceTest实现了INestedInterfaceTest接口。 我错过了什么 在没有递归接口实现的情况下,以下操作确实有效: public interface INestedInterfaceTest&l

抽象类中的GetNestedInterface方法出错。 错误消息是:类型“TNestedInterface”必须可转换为“INestedInterfaceTest”,才能将其用作泛型类“GateWay”中的参数“TNestedInterface”

但是…,我的抽象类NestedInterfaceTest实现了INestedInterfaceTest接口。 我错过了什么

在没有递归接口实现的情况下,以下操作确实有效:

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();
    }
}
公共接口INestedInterfaceTest
{
}
嵌套interfacetest的公共抽象类:inestedininterfacetest
{
公共列表子项{get;set;}
公共TNestedInterface GetNestedInterface()
其中TNestedInterface:NestedInterfaceTest,new()
{
返回GateWay.GetNestedInterface();
}
}
公共类网关
其中TNestedInterface:class,INestedInterfaceTest,new()
{
公共静态TNestedInterface GetNestedInterface()
{
返回新的TNestedInterface();
}
}

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

您在
GetNestedInterface()
上缺少一个通用约束。将其更改为:

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();
    }
}
public TNestedInterface GetNestedInterface()
其中TNestedInterface:
嵌套接口测试,
INESTEDInterfactest,//新
新的()
{
返回GateWay.GetNestedInterface();
}

注意第二个约束是新的。

NestedInterfaceTest
实现了
INestedInterfaceTest
,并且
GetNestedInterface
中对
tnestedintefacetest
的要求是它也应该实现
INestedInterfaceTest


如果我创建的
NestedInterfaceTest
子类没有重新实现
INestedInterfaceTest
接口,则该约束将不会得到满足:

public TNestedInterface GetNestedInterface<TNestedInterface>()
    where TNestedInterface : 
        NestedInterfaceTest, 
        INestedInterfaceTest<TNestedInterface>, // new
        new()
{
    return GateWay<TNestedInterface>.GetNestedInterface();
}
这样,从NestedInterfaceTest继承的任何类都将实现该约束,而无需重新实现接口,因为NestedInterfaceTest实现了该约束。这可能更容易做到:

public class GateWay<TNestedInterface>
  where TNestedInterface : class, INestedInterfaceTest<NestedInterfaceTest>, new()
公共类网关
其中TNestedInterface:class,NestedInterfaceTest,new()


不管怎么说,即使您成功地实现了这一点,对于类的使用者和(最终的)维护者来说,这仍然是令人困惑的。而且,你几乎永远也不能肯定一些误用会发生。

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

public class GateWay<TNestedInterface>
  where TNestedInterface : class, NestedInterfaceTest, new()
公共接口INestedInterfaceTest
其中TChildType:INestedInterfaceTest
{
列出子项{get;set;}
}
嵌套interfacetest的公共抽象类:inestedininterfacetest
其中TChildNestedInterface:INestedInterfaceTest
{
公共列表子项{get;set;}
公共虚拟TNestedInterface GetNestedInterface()
其中TNestedInterface:NestedInterfaceTest、INestedInterfaceTest、new()
{
返回GateWay.GetNestedInterface();
}
}
公共类网关
其中TNestedInterface:class,INestedInterfaceTest,new()
{
公共静态TNestedInterface GetNestedInterface()
{
List nestedChildren=新列表();
返回新的TNestedInterface
{
孩子们=嵌套的孩子们
};
}
}
公共类NestedClass:NestedInterfaceTest
{
公共NestedClass GetNestedClass()
{
返回GetNestedInterface();
}
}

这很好地解决了问题,并防止了接口“泄漏”。

为什么不将
GetNestedInterface
方法约束扩展到
where TNestedInterface:NestedInterfaceTest,INestedInterfaceTest,new()
?因为我不想打扰使用我为抽象类设置的接口实现抽象类的“用户”。不管我之前的观点如何,我为什么要这么做?抽象类使用该特定签名实现所需的接口。这感觉就像我在重复我自己,我不需要重复。我编辑了我的原始帖子来说明问题的本质。约束
t其中t:I
意味着
t
正是
I
,也就是说,它就在
t
类中实现,这并不意味着我可能已经回答了我自己的问题。请你复习一下好吗?我重视所有有根据的评论。请参考我给MarcinJuraszek的答案。编译器需要符合语言规范的约束。有一种观点认为,在未来的版本中,应该进行更深入的推理。这是一个非常简洁和切中要害的答案。非常感谢你!很遗憾,现在有这个限制,但我找到了一个解决方法,但是NestedInterfaceTest抽象类本身实现了所需的接口。如果我将递归接口实现替换为另一个(抽象)类,它就会工作。只有当有递归时,它才会失败。
NestedInterfaceTest
INestedInterfaceTest
,一个
Derived2
不是
INestedInterfaceTest
请容忍我,我正在努力理解。对于泛型,可以对
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>();
    }
}