为什么'IList< T>'从IReadOnlyList< T>继承? [英] Why doesn't `IList<T>` inherit from `IReadOnlyList<T>`?

查看:632
本文介绍了为什么'IList< T>'从IReadOnlyList< T>继承?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当在.NET 4.5中引入 IReadOnlyList< T> 时,我认为这个谜题的缺失部分最终被插入到位:一种通过真正的只读可索引接口,以前我不得不使用我自己的只读接口,并且围绕一切创建包装类。



我期望接口放置在自然的层次结构,理想的情况是:

  IEnumerable< T> 
.GetEnumerator()
- > IReadOnlyCollection< T> :IEnumerable< T>
.Count
- > IReadOnlyList< T> :IReadOnlyCollection< T>
。项目[...]
- >的IList< T> :IReadOnlyList< T>
.Add(...)
.Clear()
.Contains(...)
(etc)

但事实证明, IList< T> 不会从 IReadOnlyList< T>



是否有原因?

解决方案

@wb将链接指向新界面IReadOnlyList和IReadOnlyDictionary 在包含答案的评论中:


为什么我们不改变现有接口来扩展只读接口?

它看起来像是一个合理的假设,因为只读接口纯粹是读写接口的子集。不幸的是,它是不兼容的,因为在元数据级别上,每个接口上的每个方法都有自己的插槽(这使得显式接口实现可以工作)。




http://blogs.msdn.com/b/bclteam/




为了更清楚地解释这一点:假设为.NET 4.0编写的程序包含一个类 MyList< T> ,它实现 IList< T> 。显然,由于该接口不存在,因此无法实现 IReadOnlyList< T> 。现在假设系统管理员安装.NET 4.5并假设.NET 4.5使得 IList< T> 实现 IReadOnlyList< T>



如果程序将被加载,运行时会检测到 MyList< T> 声明来实现 IList< T> ,但实际上并未实现所有方法:它不实现 IReadOnlyList< T> 的方法。该程序将不再有效。



C#编译器可能能够按名称匹配这些方法,但运行时不会这样做。由于.NET 4.5应该具有向后的二进制兼容性,接口无法扩展为实现其他接口,即使其他接口包含所需方法的严格子集。


When IReadOnlyList<T> was introduced in .NET 4.5, for a moment I thought the missing part of the puzzle was finally inserted in place: a way to pass a true readonly indexable interface where previously I would have to use my own read-only interfaces and create wrapper classes around everything.

I was expecting the interface to be placed inside the "natural" hierarchy, which would ideally be:

IEnumerable<T> 
.GetEnumerator()
      -> IReadOnlyCollection<T> : IEnumerable<T>
      .Count
            -> IReadOnlyList<T> : IReadOnlyCollection<T>
            .Item[...]
                     -> IList<T> : IReadOnlyList<T>
                     .Add(...)
                     .Clear()
                     .Contains(...)
                     (etc)

But, as it turns out, IList<T> doesn't inherit from IReadOnlyList<T>.

Is there a reason for this?

解决方案

@w.b put a link to New interfaces IReadOnlyList and IReadOnlyDictionary in the comments that contains an answer:

Why did we not change the existing interfaces to extend the read-only interfaces?

It looks like a reasonable assumption that it works because the read-only interfaces are purely a subset of the read-write interfaces. Unfortunately, it is incompatible because at the metadata level every method on every interface has its own slot (which makes explicit interface implementations work).


Immo Landwerth | .NET Framework Team (BCL) | http://blogs.msdn.com/b/bclteam/

To explain this a bit more clearly:

Suppose that a program written for .NET 4.0 contains a class MyList<T> that implements IList<T>. It clearly cannot implement IReadOnlyList<T> as that interface doesn't exist.

Now suppose the system administrator installs .NET 4.5 and suppose that .NET 4.5 made IList<T> implement IReadOnlyList<T>.

If the program would then be loaded, the runtime would detect that MyList<T> claims to implement IList<T>, but doesn't actually implement all the methods: it doesn't implement IReadOnlyList<T>'s methods. The program would no longer work.

The C# compiler might be able to match the methods by name, but the runtime doesn't do this. Since .NET 4.5 was supposed to have backwards binary compatibility, interfaces couldn't be extended to implement other interfaces, not even if those other interfaces contain a strict subset of the required methods.

这篇关于为什么'IList&lt; T&gt;'从IReadOnlyList&lt; T&gt;继承?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆