C# 依赖注入转换问题
我有几个实现相同接口的类。
interface IBidManager<E> where E : IEntity
public class BidManager : IBidManager<EntityObject1>
public class BidManager2 : IBidManager<EntityObject2>
public class BidManager3 : IBidManager<EntityObject3>
在我的业务管理器类中,我有:
public class BusinessManager : ManagerBase
{
private IBidManager<IEntity> _bidLogicManager;
...
在 BusinessManager 中,我调用一个函数来更改 _bidLogicManager (MyType 是一个枚举)
public void SetLogic(MyType auctionType)
{
switch (MyType)
{
case AuctionType.Classic:
_bidLogicManager = (IBidManager<IEntity>)new BidManager();
break;
...
IEntity 是一个空白接口,我与 t4 和实体框架生成的 POCO 类一起使用。
我本可以发誓这是有效的,但我又试了一次,它抛出错误说:
Unable to cast object of type 'BLL.MyManagers.BidManager' to type 'BLL.BidManagers.IBidManager`1[Entities.IEntity]'.
当我删除 (IBidManager) 时,视觉工作室告诉我存在显式转换...但它是什么?
我不想在创建 BusinessManager 类时定义类型,如下所示:
public class BusinessManager<E> : ManagerBase where E : class, IEntity
{
IBidManager<E> _bidLogicManager;
此外,我无法在业务管理器中创建采用 IBidManager
参数的函数,因为这是一个 n 层 asp .net 应用程序,我不希望 UI 层了解有关 IBidManager 的任何信息
I have several classes that implement the same interface.
interface IBidManager<E> where E : IEntity
public class BidManager : IBidManager<EntityObject1>
public class BidManager2 : IBidManager<EntityObject2>
public class BidManager3 : IBidManager<EntityObject3>
In my business manager class I have:
public class BusinessManager : ManagerBase
{
private IBidManager<IEntity> _bidLogicManager;
...
In the BusinessManager is a function I call to change the _bidLogicManager (MyType is an enum)
public void SetLogic(MyType auctionType)
{
switch (MyType)
{
case AuctionType.Classic:
_bidLogicManager = (IBidManager<IEntity>)new BidManager();
break;
...
IEntity is a blank interface that I use with my POCO classes generated by a t4 and the Entity framework.
I could have swore this was working, but I tried it again and its throwing error sayng:
Unable to cast object of type 'BLL.MyManagers.BidManager' to type 'BLL.BidManagers.IBidManager`1[Entities.IEntity]'.
When I remove (IBidManager) visual studio tells me that an explicit conversion exists... but what is it?
I do not want to define the type when creating the BusinessManager class like so:
public class BusinessManager<E> : ManagerBase where E : class, IEntity
{
IBidManager<E> _bidLogicManager;
Also, I cannot create a function in the Business manager that takes a IBidManager<E>
argument since this is a n-layer asp.net app and I do not want the UI layer knowing anything about the IBidManager
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您可以使用.Net 4.0,您可以通过协方差解决您的转换问题。
If you can use .Net 4.0, you can solve your cast problem via covariance.