在编写我的程序时,我偶然发现了一个我无法解决的烦人问题。
在我的软件中,我有:
public interface IAutomationComponent
{
}
public class AutomationEditCollection<T> : List<T> where T : IAutomationComponent
{
}
public class AutomationMenuCollection<T> : List<T> where T : IAutomationComponent
{
}
public interface IComponentContainer
{
AutomationEditCollection<IAutomationComponent> Edits { get; }
AutomationMenuCollection<IAutomationComponent> Menus { get; }
}
public interface IAutomationEdit : IAutomationComponent
{
}
public interface IAutomationMenu : IAutomationComponent
{
}
public class AutomationEdit : IAutomationEdit
{
}
public class AutomationMenu : IAutomationMenu
{
}
(我知道很多抽象可以简化,但我只想先让它工作)
现在,我想创建一个实现IComponentContainer
的类的实例,
拥有馆藏AutomationEditCollection<AutomationEdit>
和AutomationMenuCollection<AutomationMenu>
:
public class ElementContainer : IComponentContainer
{
public AutomationEditCollection<AutomationEdit> Edits { get; } = new AutomationEditCollection<AutomationEdit>();
public AutomationMenuCollection<AutomationMenu> Menus { get; } = new AutomationMenuCollection<AutomationMenu>();
}
因此,ElementContainer
Edits = AutomationEditCollection<AutomationEdit>
想要实施AutomationEditCollection
的{{1}},应该能够Edits = AutomationEditCollection<IAutomationComponent>
实施AutomationEdit
,但我得到以下错误:
`ElementContainer.Edits'无法实现 'IComponentContainer.Edits',因为它没有匹配 返回类型'AutomationEditCollection'。 'ElementContainer.Menus'无法实现 'IComponentContainer.Menus',因为它没有匹配 返回类型'AutomationMenuCollection'。
我错过了什么?