事件和回调Events and Callbacks

回调允许框架通过委托回调用户代码来实现可扩展性。Callbacks are extensibility points that allow a framework to call back into user code through a delegate. 这些委托通常通过方法的参数传递给框架。These delegates are usually passed to the framework through a parameter of a method.

事件是回调的一种特殊情况,它支持通过便捷且一致的语法来提供委托(事件处理程序)。Events are a special case of callbacks that supports convenient and consistent syntax for supplying the delegate (an event handler). 此外,可以借助 Visual Studio 的语句完成和设计器功能来使用基于事件的 API。In addition, Visual Studio’s statement completion and designers provide help in using event-based APIs. (请参阅事件设计。)(See Event Design.)

✔️考虑使用回调来允许用户提供要由框架执行的自定义代码。✔️ CONSIDER using callbacks to allow users to provide custom code to be executed by the framework.

✔️考虑使用事件来允许用户自定义框架的行为,而无需了解面向对象的设计。✔️ CONSIDER using events to allow users to customize the behavior of a framework without the need for understanding object-oriented design.

✔️确实比普通回调更喜欢事件,因为它们对更广泛的开发人员更熟悉,并与 Visual Studio 语句完成集成。✔️ DO prefer events over plain callbacks, because they are more familiar to a broader range of developers and are integrated with Visual Studio statement completion.

❌ 避免在性能敏感的 Api 中使用回调。❌ AVOID using callbacks in performance-sensitive APIs.

在使用回调定义 Api 时,✔️会使用新的 Func<...>Action<...>Expression<...> 类型而不是自定义委托。✔️ DO use the new Func<...>, Action<...>, or Expression<...> types instead of custom delegates, when defining APIs with callbacks.

Func<...>Action<...> 表示泛型委托。Func<...> and Action<...> represent generic delegates. Expression<...> 表示可以编译并随后在运行时调用的函数定义,但也可以进行序列化并将其传递到远程进程。Expression<...> represents function definitions that can be compiled and subsequently invoked at runtime but can also be serialized and passed to remote processes.

✔️使用 Expression<...>度量并了解性能影响,而不是使用 Func<...>Action<...> 委托。✔️ DO measure and understand performance implications of using Expression<...>, instead of using Func<...> and Action<...> delegates.

在大多数情况下,Expression<...> 类型在逻辑上等同于 Func<...>Action<...> 委托。Expression<...> types are in most cases logically equivalent to Func<...> and Action<...> delegates. 它们之间的主要区别在于委托旨在用于本地处理方案;表达式适用于在远程进程或计算机中计算表达式的好处。The main difference between them is that the delegates are intended to be used in local process scenarios; expressions are intended for cases where it’s beneficial and possible to evaluate the expression in a remote process or machine.

✔️可以通过调用委托来了解这一点,因为你正在执行任意代码,这可能会对安全性、正确性和兼容性产生影响。✔️ DO understand that by calling a delegate, you are executing arbitrary code and that could have security, correctness, and compatibility repercussions.

部分©2005,2009 Microsoft Corporation。保留所有权利。Portions © 2005, 2009 Microsoft Corporation. All rights reserved.

*在 Pearson Education, Inc. 授权下,由 Addison-Wesley Professional 作为 Microsoft Windows 开发系列的一部分再版自 Framework Design Guidelines: Conventions, Idioms, and Patterns for Reusable .NET Libraries, 2nd Edition(Framework 设计准则:可重用 .NET 库的约定、惯例和模式第 2 版),由 Krzysztof Cwalina 和 Brad Abrams 发布于 2008 年 10 月 22 日。Reprinted by permission of Pearson Education, Inc. from Framework Design Guidelines: Conventions, Idioms, and Patterns for Reusable .NET Libraries, 2nd Edition by Krzysztof Cwalina and Brad Abrams, published Oct 22, 2008 by Addison-Wesley Professional as part of the Microsoft Windows Development Series.

另请参阅See also