没有两个类型参数共享父项和子项的装箱转换 [英] No Boxing Conversion for two type parameters shared Parent and Child

查看:159
本文介绍了没有两个类型参数共享父项和子项的装箱转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



错误1类型'T'不能用作类型泛型类型或方法'... Child'中的参数'T'。没有从'T'到'... IParent'的装箱转换或类型参数转换。

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

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

$ / code>

我想要任何类继承IParent,并为每个这样的类构造一个



T是继承IParent的类,Id是该类的父范围中的枚举。



  public> 

界面IParent< T,Id>其中T:IParent< T,Id>
{
儿童< T,Id> GetChild();
}

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

public Child< T,Id> GetChild()
{
throw new NotImplementedException();
}
}


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.

解决方案

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();
    }
}

这篇关于没有两个类型参数共享父项和子项的装箱转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆