gpt4 book ai didi

C# ReSharper : Possible infinite inheritance

转载 作者:行者123 更新时间:2023-11-30 18:15:33 25 4
gpt4 key购买 nike

为什么 ReSharperIGeneric<TGeneric> 上发出关于可能的无限继承 的警告|但不在ISimple接口(interface)?

// api

public interface INodeBase<TNode>
where TNode : INodeBase<TNode>
{
TNode Parent { get; set; }
List<TNode> Children { get; set; }
}

public interface INode<TValue>
: INodeBase<INode<TValue>>
{
TValue Value { get; set; }
}

public interface IBelongToNodeBase<TOwner>
where TOwner : INodeBase<TOwner>
{
TOwner Owner { get; set; }
}

// All good
public interface ISimple
: IBelongToNodeBase<INode<ISimple>>
{
}

// Possible infinite inheritance
public interface IGeneric<TGeneric>
: IBelongToNodeBase<INode<IGeneric<TGeneric>>>
{
}

这可能只是 ReSharper 智能感知的问题吗?这来自真实案例场景,代码编译和运行没有任何问题。

最佳答案

基于link (由 René 提供),ReSharper 文档在泛型引用自身的三层嵌套之后提出了这个问题。

In a situation similar to the following:

class B<U>
{
}
class A<T> : B<A<A<T>>>
{
}

You effectively end up with the type A that inherits an infinitely recursive type B. As a result, your assembly will compile, but you will be unable to execute it. If you try, you will get an error message similar to the following:

Could not load type ‘ConsoleApplication1.A{{1' from assembly 'ConsoleApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' because it has recursive generic definition.

关于C# ReSharper : Possible infinite inheritance,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47923724/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com