想做IGenericInterface <T>其中T:ISomethingElse <typeof(this)> //即实现类的类型

dFl*_*lat 0 c# generics where where-clause

我想使用的this关键字或类似的东西typeof(this)where我的通用接口的限制,但显然这不是正确的(没有编译).有一种光滑的方式可以做到这一点,我不知道吗?

interface IParent<TChild> where TChild : IChildOf<typeof(this)>
{

    void AddRange(TChild children){}

}

interface IChildOf<TParent> : IDisposable
{
    TParent Parent { get; }
}
Run Code Online (Sandbox Code Playgroud)

或者我必须这样做

interface IParent<TChild, T2> where TChild : IChildOf<T2>
Run Code Online (Sandbox Code Playgroud)

并且只知道T2将是实现接口的类?

Ste*_*ary 6

这里可以使用奇怪的重复通用模式:

interface IParent<TChild, TParent>
  where TChild : IChildOf<TParent>
  where TParent : IParent<TChild, TParent>
{
  void AddRange(TChild children);
}
Run Code Online (Sandbox Code Playgroud)

但我会认真考虑重新评估你的设计.你真的需要这个吗?