No Boxing Conversion for two type parameters share

2019-08-10 04:03发布

I get the following error (VSC#2010 Express) on the declaration of GetChild method...

Error 1 The type 'T' cannot be used as type parameter 'T' in the generic type or method '...Child'. There is no boxing conversion or type parameter conversion from 'T' to '...IParent'.

public interface IParent<T, Id>
{
    Child<T, Id> GetChild();
}

public class Child<T, Id> where T : IParent<T, Id>
{
    public T Parent;
}

I want any class to inherit IParent, and for each such class to construct a member instance of Child.

T is the class inheriting IParent, and Id is an enum in the parent scope of that class.

1条回答
戒情不戒烟
2楼-- · 2019-08-10 04:25

Try constraining type T in the interface as well.

public interface IParent<T, Id> where T : IParent<T, Id>
{
    Child<T, Id> GetChild();
}

public class Child<T, Id> where T : IParent<T, Id>
{
    public T Parent;

    public Child<T, Id> GetChild()
    {
        throw new NotImplementedException();
    }
}
查看更多
登录 后发表回答