Unity :2017.3.0f3(在5.x上相同)
我遇到了与这个问题完全相同的问题: Storing list of generic class of derived objects
public interface Animal { void bar(); }
public interface IContainer <out T> where T : Animal { void foo();}
public class Cat : Animal{ void bar(){} }
public class CatContainer : IContainer<Cat>{ void foo(){} }
/* ... */
public void test () {
List<IContainer<Animal> list= new List<IContainer<Animal>> ();
list.Add (new CatContainer()); //Error
}
当我将new CatContainer()
推入列表时,我遇到了这个例外:
ArrayTypeMismatchException: Source array type cannot be assigned to destination array type.
(wrapper stelemref) object:stelemref (object,intptr,object)
注意:此代码适用于Unity。
你也有这个问题吗?有解决方法吗?