简体   繁体   English

回报类型的差异

[英]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 type OtherType<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.

相关问题 为什么类类型参数的方差必须与其方法的返回/参数类型参数的方差相匹配? - Why does the variance of a class type parameter have to match the variance of its methods' return/argument type parameters? 通用类型约束和方差 - Generic type constraint and variance 类型参数的C#variance注释,约束为值类型 - C# variance annotation of a type parameter, constrained to be value type 我在C#中的方差函数未返回准确值 - My variance function in C# does not return accurate value out关键字如何与类型协方差关联? - How is out keyword associated to type co-variance? C#3中具有接口继承(co(ntra) - 方差?)的通用类型推断 - Generic type inference with interface inheritance (co(ntra)-variance?) in C# 3 方差无效:type参数必须是无变量有效的,但是是协变的 - Invalid variance: The type parameter must be invariantly valid but is covariant 需要协变返回类型和“真实”class 方差的语言 Scala 如何在 CLR 上运行? - How do languages Scala which need covariant return types and “real” class variance run on the CLR? 如何解决这个错误? 方差无效:类型参数“T”必须是无效的 - How to fix this error? Invalid variance: The type parameter 'T' must be invariantly valid on 对于更高类型的类型,C#4.0中泛型类型参数的方差是否更接近? - Is variance of for generic type parameters in C# 4.0 a step closer for higher kind types?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM