问题
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:
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();
}
}
来源:https://stackoverflow.com/questions/24543043/no-boxing-conversion-for-two-type-parameters-shared-parent-and-child