我有 2 个具有以下声明的类:
abstract class ClassBase<T, S> where T : myType where S : System.Data.Objects.DataClasses.EntityObject
abstract class ServiceBase<T> where T : myType
我还有 2 个其他类,每个类都继承一个,我们可以调用ClassInherited和ServiceInherited。请注意,这两个 Service 类与其他两个不在同一个项目中。
这个想法是,ServiceBase
我可以在类中声明一个属性protected ClassBase<T,System.Data.Objects.DataClasses.EntityObject> Class { get; set; }
,然后在继承的服务的构造函数中声明类似this.Class = ClassInheritedInstance
我已经实现了这个想法,但是在 ServiceInherited 类构造函数中分配Class属性时它给了我这个错误:
无法将类型“ClassInherited”隐式转换为“ClassBase< T, S>”
请注意, ClassInherited 确实是Class<T,S>
……的规范,只是编译器似乎无法正确分辨类型。还将类属性的声明更改为protected ClassBase<T, EntityObjectInherited>
有效,而EntityObjectInherited是System.Data.Objects.DataClasses.EntityObject
...的实现。我不明白为什么会出现问题。
更新 1
请注意,在编译时类型ClassInherited
是已知的,因为它的声明是public class ClassInherited : ClassBase<myTypeInherited, EntityObjectInherited>