查看文章 |
一篇难得的好文章,关于.Net的Delegates,写的很生动,很易理解。容许我稍后再把它翻译出来。 The following is an excerpt from Windows Forms 2.0 Programming, Chris Sells & Michael Weinhardt, Addison-Wesley, 2006. It's been updated from the original version for C# 2.0. Anonymous DelegatesAt this point, Peter is using delegates to notify interested parties in the process of his work and using delegates to get notified when grades are available on the work he�s completed. The delegates provided by his boss and the universe are provided by separate entities, so it makes sense that they are encapsulated in methods on those entities. However, in the case of the WorkGraded method, there�s really no good reason for this to be a separate method except the syntactic requirements of C# 1.0. As of C# 2.0, Peter can drop the code required to handle the processing of his work grade into an anonymous delegate: class Worker { Here, instead of passing in the name of a method to call when his work has been graded, he�s passing in the body of the method itself as designated with a different use of the delegate keyword to create a method with no name (and therefore �anonymous�). The body of the method is fundamentally the same in that Peter still passes the WorkCompleted delegate as a parameter to BeginInvoke and then pulls it out of AsyncState for use in extracting the result. However, one of the benefits of anonymous delegates that Peter knows is that he can make use of the variables in the surrounding context from within the anonymous delegate body, causing him to rewrite his code thusly: class Worker { This code compiles just fine, but when it�s run, it will cause the following exception to be thrown: System.InvalidOperationException: The IAsyncResult object provided does not match this delegate. The problem is that while the wc variable is allowed to be used in the anonymous delegate, it�s still being used by the for-each statement. As soon as the asynchronous invocation begins, the wc variable changes and the delegate used to start things (wc) no longer matches the async result passed as an argument to the anonymous delegate. Peter slaps his head and creates a hybrid solution: class Worker { Happiness in the UniversePeter, his boss and the universe are finally satisfied. Peter's boss and the universe are allowed to be notified of the events that interest them, reducing the burden of implementation and the cost of unnecessary round-trips. Peter can notify them each, ignoring how long it takes them to return from their target methods, while still getting his results asynchronously and handling them using anonymous delegates, resulting in the following complete solution: delegate void WorkStarted(); Peter knows that getting results asynchronously comes with issues, because as soon as he fires events asynchronously, the target methods are likely to be executed on another thread, as is Peter's notification of when the target method has completed. However, Peter is good friends with Mike, who is very familiar with threading issues and can provide guidance in that area. And they all lived happily every after. The end. 转载自 Sells Brothers -- Chris's Home on the Web |