C# Unity IoC引发无效的强制转换异常

C# Unity IoC引发无效的强制转换异常,c#,xamarin.forms,unity-container,C#,Xamarin.forms,Unity Container,我正试图通过Unity IoC注册一个通用接口/实现,但是ViewModel中的构造函数参数抛出了一个无效的强制转换异常,我不知道为什么 我有一个接口,所有模型都实现该接口: public interface IEntity {} 典型的模型如下所示: public class Dashboard: IEntity { .... } container.RegisterType(typeof(IDbService<>), typeof(DbService<>));

我正试图通过Unity IoC注册一个通用接口/实现,但是ViewModel中的构造函数参数抛出了一个无效的强制转换异常,我不知道为什么

我有一个接口,所有模型都实现该接口:

public interface IEntity {}
典型的模型如下所示:

public class Dashboard: IEntity { .... }
container.RegisterType(typeof(IDbService<>), typeof(DbService<>));
private readonly IDbService<Dashboard> _dbService;

public DashboardPageViewModel(IDbService<Dashboard> dbService)
{
     _dbService = dbService;
}
我有一个数据库服务,可以是任何模型类型,还有一个关联的接口,例如:

public interface IDbService<T> where T: IEntity { .... }

public class DbService<T> where T : IEntity, IDbService<T> { .... }
公共接口IDbService,其中T:IEntity{..}
公共类DbService,其中T:IEntity,IDbService{…}
容器注册如下所示:

public class Dashboard: IEntity { .... }
container.RegisterType(typeof(IDbService<>), typeof(DbService<>));
private readonly IDbService<Dashboard> _dbService;

public DashboardPageViewModel(IDbService<Dashboard> dbService)
{
     _dbService = dbService;
}
container.RegisterType(typeof(IDbService),typeof(DbService));
到目前为止都是这样

但是,当我尝试将其加载到我的ViewModel类中时,我在DashboardPageViewModel中得到一个无效的强制转换异常,我在构造函数上的实现(为了参数),如下所示:

public class Dashboard: IEntity { .... }
container.RegisterType(typeof(IDbService<>), typeof(DbService<>));
private readonly IDbService<Dashboard> _dbService;

public DashboardPageViewModel(IDbService<Dashboard> dbService)
{
     _dbService = dbService;
}
private readonly IDbService\u dbService;
公共仪表板页面视图模型(IDbService dbService)
{
_dbService=dbService;
}
这会引发异常


为何我不能在这方面作出回应?在注册类型后尝试解析强制转换也不起作用。

该类定义不正确


该类的定义不正确