C# 依赖注入问题
我有 3 个类:A、B 和 C
所有这些类都实现一个接口 IMyInterface
我希望像这样定义该接口:
internal IMyInterface<E> where E: class
{
E returnData();
}
以便它可以返回 E 类型的数据。 类型“E”将是使用实体框架 v4 创建的 POCO 对象。
在一个单独的类中,我有:
public class MyClass()
{
IMyInterface<??> businessLogic;
public setBusinessLogic(IMyInterface<E> myObject)
where E : class
{
businessLogic = myObject;
}
}
我尝试将
我尝试让我的实体实现一个空接口 IEntity
然后使用
IMyInterface<IEntity> businessLogic;
...
businessLogic = new A<POCOObject>();
结果:
Cannot implicitly convert type
'A<POCOObject>' to
'IMyInterface<IEntity>'. An explicit
conversion exists (are you missing a
cast?)
有什么建议吗?
编辑:我尝试将我的 A、B 和 C 类声明为:
internal class A : IBidManager<EntityObjectType>
并
internal class A<E> : IBidManager<E> where E : class
导致相同的错误。
I have 3 classes: A, B, and C
All of these classes implement an interface IMyInterface
I would like the interface to be defined like so:
internal IMyInterface<E> where E: class
{
E returnData();
}
So that it can return data of type E.
The type "E" will be a POCO object created with the Entity Framework v4.
In a separate class I have:
public class MyClass()
{
IMyInterface<??> businessLogic;
public setBusinessLogic(IMyInterface<E> myObject)
where E : class
{
businessLogic = myObject;
}
}
I tried putting <object>
in place of <??>
but it could not cast my poco entity type.
I tried having my entities implement an empty interface IEntity
then using
IMyInterface<IEntity> businessLogic;
...
businessLogic = new A<POCOObject>();
results in:
Cannot implicitly convert type
'A<POCOObject>' to
'IMyInterface<IEntity>'. An explicit
conversion exists (are you missing a
cast?)
Any recommendations?
Edit: I've tried declaring my A, B, and C classes as:
internal class A : IBidManager<EntityObjectType>
and
internal class A<E> : IBidManager<E> where E : class
results in the same error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它必须是
或
如果您希望业务对象处理任何 POCO 对象并且它们每个对象都有一个接口,那么您将需要在类级别指定
where E : class, IEntity
。否则你必须使用具体类型作为通用参数It will have to be either
or
If you wnt your business object to handle any POCO object and they each have an interface then you will need to specify
where E : class, IEntity
at the class level. Otherwise you have yo use concrete type for the generic arg