为什么不BeginInvoke的返回类型AsyncResult的参考? [英] Why doesn’t BeginInvoke return reference of type AsyncResult?

查看:159
本文介绍了为什么不BeginInvoke的返回类型AsyncResult的参考?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当你调用一个委托对象的BeginInvoke的,系统会创建类型AsyncResult的对象,但BeginInvoke的返回类型为IAsyncResult接口的参考(指向AsyncResult实例)。为什么不BeginInvoke的,而不是返回类型AsyncResult的参考?

When you call a delegate object’s BeginInvoke, the system creates an object of type AsyncResult, but BeginInvoke returns a reference of type IAsyncResult interface ( which points to AsyncResult instance) . Why doesn’t BeginInvoke instead return a reference of type AsyncResult?

感谢名单

推荐答案

这是标准的接口编程基础

通过返回一个IAsyncResult,该框架是自由改变内部实现在稍后的时间,不破坏code写的对框架的API。这在本质上,告诉我们,作为开发商,我们不应该关心什么类型的实现时,提供(IAsyncResult的)满足的接口。

By returning an IAsyncResult, the framework is free to change the internal implementation at a later time, without breaking code written against the framework's API. This, in essence, telling us, as developers, that we should not care what type of implementation is used, provided the interface (IAsyncResult) is met.

相反,如果被返回的实际AsyncResult类,然后更改为不同的类会破坏现有的code。

If, instead, the actual AsyncResult class was returned, then changing to a different class would break existing code.

此外,这允许框架使用相同的接口(IAsyncResult的)在多个地方,所述AsyncResult类是否是适当的。我已经采取利用了这一点,就个人而言,我做了它返回一个不同的类,它存储这是对我很重要的信息IAsyncResult的支持我自己的异步功能。这让我自己code的工作,如框架,不限制我的框架的执行情况。

Also, this allows the framework to use the same interface (IAsyncResult) in multiple places, whether or not the AsyncResult class is appropriate. I've taken advantage of this, personally, as I've made my own asynchronous functions which return IAsyncResult backed by a different class, which stored the information which was important to me. This allows my own code to work like the framework, without restricting me to the framework's implementation.

这篇关于为什么不BeginInvoke的返回类型AsyncResult的参考?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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