[英]Variance in return type
EDIT: Maybe this is a clearer, more to the point formulation of the question: 编辑:也许这是一个更清晰的问题,更多的是问题的重点:
In some generic interface IInterface<T>
, I want to return an object of a generic type, where one of the type arguments should be an implementation of IInterface<T>
. 在一些通用接口
IInterface<T>
,我想返回一个泛型类型的对象,其中一个类型参数应该是IInterface<T>
。
public class OtherType<T> {}
public interface IInterface<T>
{
OtherType<IInterface<T>> Operation();
}
public class Impl : IInterface<int>
{
public OtherType<IInterface<int>> Operation()
{
return new OtherType<Impl>();
}
}
Since Impl
implements IInterface<int>
, it seems reasonable to me that I could use it this way. 由于
Impl
实现了IInterface<int>
,因此我可以通过这种方式使用它。 Yet, it seems I cannot, I get the compiler error 然而,似乎我不能,我得到编译器错误
Cannot convert expression type
OtherType<Impl>
to to return typeOtherType<IInterface<int>>
无法将表达式类型
OtherType<Impl>
转换为返回类型OtherType<IInterface<int>>
OtherType<IInterface<int>>
doesn't mean "implements" - it sort of means "is a type OtherType
with a generic type parameter Interface<int>
, but that isn't how you say it. OtherType<IInterface<int>>
并不意味着“implements” - 它的意思是“是一个带有泛型类型参数Interface<int>
OtherType
类型,但这不是你怎么说的。
If you just want to make sure that the return type implements IInterface<int>
then set that as the return type: 如果您只是想确保返回类型实现
IInterface<int>
那么将其设置为返回类型:
public interface IInterface<T>
{
IInterface<T> Operation();
}
public class Impl : IInterface<int>
{
public <IInterface<int>> Operation()
{
return new OtherType();
}
}
where 哪里
public class OtherType : IInterface<int>
{}
This means you can return any type that implements IInterface<int>
. 这意味着您可以返回任何实现
IInterface<int>
。
Otherwise you can make it a little more constrained on calling use a generic type constraint: 否则,您可以在调用使用泛型类型约束时使其更受限制:
public interface IInterface<T>
{
TRet Operation<TRet>() where TRet : IInterface<T>;
}
public class Impl : IInterface<int>
{
public TRet Operation<TRet>() where TRet : IInterface<int>
{
return new OtherType();
}
}
This means that you can constraint the operation to return a particular class, which has in turn to implement IInterface<int>
. 这意味着您可以约束操作以返回特定的类,而该类又实现了
IInterface<int>
。
It would be called: 它将被称为:
Impl i = new Impl();
OtherType x = i.Operation<OtherType>();
The issue is that OtherType<T>
is a class and generic classes do not allow co/contravariance in C#. 问题是
OtherType<T>
是一个类,泛型类不允许C#中的共同/逆转。 Generic interfaces
do, as long as out
types do not appear in any input positions, and in
types do not appear in any output positions. 通用
interfaces
做,只要out
类型不会在任何输入位置出现,而in
类型不会出现在任何输出位置。 In your code sample, you could get it to compile by introducing an additional interface marked covariant, and then altering your return type. 在您的代码示例中,您可以通过引入标记为covariant的附加接口,然后更改返回类型来进行编译。
public interface IOtherType<out T> {} // new
public class OtherType<T> : IOtherType<T> { }
public interface IInterface<T>
{
IOtherType<IInterface<T>> Operation(); // altered
}
public class Impl : IInterface<int>
{
public IOtherType<IInterface<int>> Operation()
{
return new OtherType<Impl>();
}
}
Whether or not this would actually fit your use case with your additional method definitions is something only you can know, given the limited about of detail in your code snippet. 考虑到代码片段中的细节有限,这是否真的适合您的用例以及其他方法定义,这是您可以知道的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.