简体   繁体   English

具有泛型参数的转换接口由类实现,而没有泛型参数

[英]Casting interface with generic parameter implemented by class without generic parameter

I have two interfaces, 我有两个界面,

public interface IDocument<SegType> where SegType : ISegment

and

public interface ISegment

Basically, I want to enforce that each class that implements IDocument is composed of one type of ISegment , and that people using these classes never need to know about which type of ISegment their class uses internally. 基本上,我想强制实现每个实现IDocument类都由一种类型的ISegment ,并且使用这些类的人们永远不需要知道他们的类在内部使用哪种类型的ISegment

I then created a class that implements IDocument<SegType> where SegType : ISegment : 然后,我创建了一个实现IDocument<SegType> where SegType : ISegment的类, IDocument<SegType> where SegType : ISegment

public class MyDocument : IDocument<MySegment>

and the corresponding MySegment : 和相应的MySegment

public class FffSegment : ISegment

All of this compiles and works just as I expect it to when I specify MyDocument as the type. MyDocument指定为类型时,所有这些编译和工作均与我期望的一样。 Why can I not implicitly cast an instance of MyDocument to be of type IDocument<Isegment> ? 为什么不能将MyDocument的实例隐式转换为IDocument<Isegment>类型? When I try the line: 当我尝试该行时:

IDocument<ISegment> doc = new MyDocument();

I get the error 我得到错误

Cannot implicitly convert type 'MyDocument' to 'IDocument<ISegment>'. An explicit conversion exists (are you missing a cast?)

But when I cast it, it just comes back null. 但是当我投射它时,它只是返回null。 If I change it to: 如果我将其更改为:

IDocument<MySegment> doc = new MyDocument();

it works, as it does when I change the class definition to 它可以正常工作,就像我将类定义更改为

public class MyDocument : IDocument<ISegment>

Why can I not force a specific type that implements ISegment for that implementation of IDocument? 为什么不能强制为IDocument的实现强制实现实现ISegment的特定类型? I want to re-use this code for different types of IDocument and ISegment and maybe some day some implementations of IDocument will allow for multiple types of ISegment but MyDocument should be restricted against this kind of behavior. 我想针对不同类型的IDocumentISegment重新使用此代码,也许有一天IDocument某些实现将允许多种类型的ISegment但是MyDocument应该限制​​这种行为。 How can I enforce these requirements but still write code that is generic enough to be re-used in the future? 如何执行这些要求,但仍然编写通用的代码以备将来重用?

You need to get into the nastiness of co-and-contra variance: 您需要深入了解逆方差:

public interface IDocument<out SegType> where SegType : ISegment
{}

I think that should make your example compile now... 我认为这应该使您的示例现在可以编译...

Here's some reading material: 这是一些阅读材料:

Because IDocument<MySegment> cannot cast directly to an IDocument<ISegment> . 因为IDocument<MySegment>无法直接转换为IDocument<ISegment> If it could, you could do this: 如果可以,您可以这样做:

IDocument<ISegment> doc1 = new IDocument<MySegment>;
doc1.Add(new MyOtherSegment());   // invalid

(Assuming IDocument<T> has an Add(T newSegment) method) (假设IDocument<T>具有Add(T newSegment)方法)

Take a long look at your design and decide if generics are really necessary or if just using ISegment within IDocument is sufficient. 仔细研究一下您的设计,并确定泛型是否确实必要,或者仅在IDocument使用ISegment就足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM