我知道这是旧的,但我仍然不太了解这些问题.任何人都可以告诉我为什么以下不起作用(抛出一个runtime关于铸造的例外)?
public abstract class EntityBase { }
public class MyEntity : EntityBase { }
public abstract class RepositoryBase<T> where T : EntityBase { }
public class MyEntityRepository : RepositoryBase<MyEntity> { }
Run Code Online (Sandbox Code Playgroud)
而现在的铸造生产线:
MyEntityRepository myEntityRepo = GetMyEntityRepo(); // whatever
RepositoryBase<EntityBase> baseRepo = (RepositoryBase<EntityBase>)myEntityRepo;
Run Code Online (Sandbox Code Playgroud)
那么,任何人都可以解释这是如何无效的?而且,我没有心情去解释 - 是否有一行代码我可以用来实际进行演员表演?