[英]Interface implementation fails for generic type, because of “does not have the matching return type” error
I have a class: 我有一堂课:
public abstract class Structure<T, S> : IStructure
where T : StructureModel<S>, new()
where S : StructureStats, new()
{
protected T _model;
public S Stats { get { return _model.Stats; } set { _model.Stats = value; } }
}
and an interface: 和一个接口:
public interface IStructure
{
StructureStats Stats{ get; set;}
}
The code fails to compile, with the message 代码无法编译,并显示以下消息
Error 28 'Server.Models.Structures.Structure' does not implement interface member 'Server.Models.Structures.IStructure.Stats'.
错误28'Server.Models.Structures.Structure'未实现接口成员'Server.Models.Structures.IStructure.Stats'。 'Server.Models.Structures.Structure.Stats' cannot implement 'Server.Models.Structures.IStructure.Stats' because it does not have the matching return type of 'Server.Models.Structures.StructureStats'.
“ Server.Models.Structures.StructureStats”无法实现“ Server.Models.Structures.IStructure.Stats”,因为它没有匹配的“ Server.Models.Structures.StructureStats”返回类型。 C:\\SRDevGit\\freecon-galactic-server\\SRServer\\Server.Models\\Structures\\Structure.cs
C:\\ SRDevGit \\ freecon星系服务器\\ SRServer \\ Server.Models \\结构\\ Structure.cs
I am unsure as to what the problem is; 我不确定是什么问题; the return type of Structure.Stats {get;} should be guaranteed to match the interface definition, since generic type S is defined as deriving from StructureStats by
应该保证Structure.Stats {get;}的返回类型与接口定义匹配,因为通用类型S被定义为通过以下方式从StructureStats派生
where S:StructureStats, new()
What am I missing? 我想念什么? I would like to avoid making IStructure generic, if possible, as it would complicate collections of
如果可能的话,我想避免使IStructure通用,因为它会使
<IStructure<GenericType>>
because GenericType is a polymorphic class. 因为GenericType是一个多态类。
C# is strongly typed, and does not support covariant return types, so to inherit from IStructure, Structure has to return the exact signature StructureStats Stats{ get; set;}
C#
是强类型的,并且不支持协变返回类型,因此要从IStructure继承,Structure必须返回确切的签名StructureStats Stats{ get; set;}
StructureStats Stats{ get; set;}
. StructureStats Stats{ get; set;}
。 Something like StructureStatsDerived Stats{ get; set;}
类似于
StructureStatsDerived Stats{ get; set;}
StructureStatsDerived Stats{ get; set;}
is not an exact match and the compiler rejects it. StructureStatsDerived Stats{ get; set;}
不是完全匹配项,编译器会拒绝它。
It looks like you don't actually need the generic parameter S. You could drop the S and just return StructureStats. 看起来您实际上并不需要通用参数S。您可以删除S并仅返回StructureStats。
There's no way to achieve it without use a generic interface, also you need to switch the order in generic constraints for Structure<T, S>
没有使用通用接口就无法实现它,您还需要在通用约束中切换
Structure<T, S>
的顺序
public class Structure< T, S> : IStructure<S>
where S : StructureStats, new()
where T : StructureModel<S>, new()
{
protected T _model;
public S Stats { get { return _model.Stats; } set { _model.Stats = value; } }
}
public interface IStructure<S> where S : StructureStats
{
S Stats { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.