[英]Return generic interface implementation with different generic type
I have created this simple generic interface:我创建了这个简单的通用接口:
public interface IInitializerSettings<in ViewerType> where ViewerType : Component
{
void Apply(ViewerType dataViewer);
}
And added an implementation for it:并为其添加了一个实现:
public class MenuSettings : IInitializerSettings<CustomGridLayout>
{
public void Apply(CustomGridLayout dataViewer)
{
Debug.Log("Applied");
}
}
public class CustomGridLayout : CustomLayout
{
// The implementation code
}
Now I try to use it like that:现在我尝试这样使用它:
public IInitializerSettings<CustomLayout> GetDefaultSettings()
{
return new MenuSettings();
}
But I get this error "Cannot convert type MenuSettings to return type IInitializerSettings"但我收到此错误“无法将类型 MenuSettings 转换为返回类型 IInitializerSettings”
I don't understand why it isn't allowed, CustomGridLayout
inherits CustomLayout
.我不明白为什么不允许,
CustomGridLayout
继承CustomLayout
。
All I could find is this question , but this solution doesn't work for me (I can't use the out
keyword).我能找到的只是这个问题,但这个解决方案对我不起作用(我不能使用
out
关键字)。
The reason you cannot do this is because for a contravariant interface (specified by your use of in
for the generic type parameter) you cannot implicitly convert it to an instance of a less derived type.您不能这样做的原因是,对于逆变接口(通过使用
in
为泛型类型参数指定),您无法将其隐式转换为派生程度较低的类型的实例。 I think the bullet points in the docs explains it fairly ok, if you think in terms of IEnumerable<T>
(covariant) and Action<T>
(contravariant).如果您从
IEnumerable<T>
(协变)和Action<T>
(逆变)的角度考虑,我认为文档中的要点解释得相当好。
As Selvin mentions in the comments the Apply
method in MenuSettings
expects an instance of CustomGridLayout
, so trying to cast MenuSettings
to IInitializerSettings<CustomLayout>
is not possible because public void Apply(CustomGridLayout dataViewer)
cannot handle a CustomLayout
as input.正如 Selvin 在评论中提到的那样,
MenuSettings
中的Apply
方法需要一个CustomGridLayout
的实例,因此无法尝试将MenuSettings
为IInitializerSettings<CustomLayout>
,因为public void Apply(CustomGridLayout dataViewer)
无法将CustomLayout
作为输入处理。 Let me give an example:让我举个例子:
public class CustomLayout
{
public void SetupCustomLayout() { ... }
}
public class CustomGridLayout : CustomLayout
{
public void SetupGrid() { ... }
}
public class MenuSettings : IInitializerSettings<CustomGridLayout>
{
public void Apply(CustomGridLayout dataViewer)
{
dataViewer.SetupGrid();
}
}
// Later in the code...
var menuSettings = new MenuSettings();
// This cast is what GetDefaultSettings() is trying to do
var genericSettings = (IInitializerSettings<CustomLayout>)menuSettings;
var layout = new CustomLayout();
// Looking at the type of 'genericSettings' this following line should be possible
// but 'MenuSettings.Apply()' is calling 'dataViewer.SetupGrid()' which doesn't exist
// in 'layout', so 'layout' is not a valid input
genericSettings.Apply(layout);
So in relation to the docs you have defined IInitializerSettings<ViewerType>
as a contravariant interface, but are trying to use it as a covariant interface - which is not possible.因此,关于文档,您已将
IInitializerSettings<ViewerType>
定义为逆变接口,但正试图将其用作协变接口 - 这是不可能的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.