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

查看:234
本文介绍了为什么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?

thanx

推荐答案

这是标准的基于界面的编程

通过返回IAsyncResult,框架可以随时更改内部实现,而不会破坏针对框架的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类,然后更改为不同的类将破坏现有代码。

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

此外,这允许框架在多个中使用相同的接口(IAsyncResult)无论AsyncResult类是否适合,都可以。我已经利用了这一点,因为我已经做了我自己的异步函数,它返回由不同的类支持的IAsyncResult,它存储了对我很重要的信息。这允许我自己的代码像框架一样工作,而不必限制框架的实现。

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天全站免登陆