C#ReSharper:可能的无限继承

问题描述 投票:3回答:1

为什么ReSharper会对IGeneric<TGeneric>上的可能的无限继承发出警告,但不会在ISimple接口上发出警告?

// 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智能感知的一个问题吗?这来自实际案例场景,代码编译和运行没有任何问题。

c# inheritance resharper
1个回答
0
投票

基于link(由René提供),ReSharper文档在三个嵌套级别(通用引用自身)之后引发了此问题。

在类似于以下的情况下:

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

你有效地得到了继承无限递归类型B的类型A.结果,你的程序集将编译,但你将无法执行它。如果您尝试,您将收到类似于以下内容的错误消息:

无法加载类型'ConsoleApplication1.A {{1'来自程序集'ConsoleApplication1,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null',因为它具有递归通用定义。

© www.soinside.com 2019 - 2024. All rights reserved.