【发布时间】:2011-05-07 04:15:08
【问题描述】:
interface IModel {}
class MyModel : IModel {}
interface IRepo<T>
where T: IModel { }
class Repo : IRepo<MyModel> { }
//EDIT: A smaller example
IRepo<IModel> repo = new Repo(); // Cannot implicitly convert.. An explicit convertion exists. Missing cast?
// Old example:
/*
The type 'Repo' cannot be used as type parameter 'C' in the generic type or method.
'Castle.MicroKernel.Registration.ComponentRegistration<S>.ImplementedBy<C>()'.
==> There is no implicit reference conversion from 'Repo' to 'IRepo<IModel>'.
*/
container.Register(
Component.For<IRepo<IModel>>()
.ImplementedBy<Repo>());
但是 Repo 是从 IRepo 派生的,而 MyModel 是从 IModel 派生的。为什么这不起作用?
我尝试在 Repo 上添加一个隐式运算符,但不允许在接口之间转换..
这是否通过 c#4 中的 co/contra varience 解决了(不,我不知道我在说什么 :))?
【问题讨论】:
标签: .net-3.5 inheritance interface c#-3.0