public class Base
{
}
public class Rabbit : Base
{
}
public abstract class OO
{
public abstract ICollection<T> Test<T>() where T : Base;
}
public class LOL : OO
{
public override ICollection<T> Test<T>()
{
return new Collection<Rabbit>();
}
}
这种类的形成会产生以下错误。
&#39; System.Collections.ObjectModel.Collection&#39;到&#39; System.Collections.Generic.ICollection&#39;。存在显式转换(您是否错过了演员?)
知道如何解决这个问题吗?
答案 0 :(得分:1)
试试这个:
public class LOL : OO
{
public override ICollection<T> Test<T>()
{
return (ICollection<T>) new Collection<Rabbit>();
}
}
答案 1 :(得分:0)
public class Base
{
}
public class Rabbit : Base
{
}
public abstract class OO
{
public abstract ICollection<T> Test<T>() where T : Base;
}
public class LOL : OO
{
public override ICollection<T> Test<T>()
{
return new Collection<T>();
}
}
或
public class Base
{
}
public class Rabbit : Base
{
}
public abstract class OO
{
public abstract ICollection<T> Test<T>() where T : Base;
}
public class LOL : OO
{
public override ICollection<T> Test<T>()
{
return new Collection<Rabbit>() as ICollection<T>;
}
}