Information Technology Reference
In-Depth Information
It works as a single delegate, but using it as a multicast is problematic:
Func < bool > cp = () => CheckWithUser();
cp += () => CheckWithSystem();
c.LengthyOperation(cp);
The value returned from invoking the delegate is the return value from
the last function in the multicast chain. All other return values are ignored.
The return from the CheckWithUser() predicate is ignored.
Yo u a d d r e s s b o t h i s s u e s b y i n v o k i n g e a c h d e l e g a t e t a r g e t y o u r s e l f . E a c h
delegate you create contains a list of delegates. To examine the chain your-
self and call each one, iterate the invocation list yourself:
public void LengthyOperation2( Func < bool > pred)
{
bool bContinue = true ;
foreach ( ComplicatedClass cl in container)
{
cl.DoLengthyOperation();
foreach ( Func < bool > pr in pred.GetInvocationList())
bContinue &= pr();
if (!bContinue)
return ;
}
}
In this case, I've defined the semantics so that each delegate must be true
for the iteration to continue.
Delegates provide the best way to utilize callbacks at runtime, with simpler
requirements on client classes. You can configure delegate targets at run-
time. You can support multiple client targets. Client callbacks should be
implemented using delegates in .NET.
Item 25: Implement the Event Pattern for Notifications
The .NET Event Pattern is nothing more than syntax conventions on the
Observer Pattern. (See Design Patterns , Gamma, Helm, Johnson, and
Vlissides pp. 293-303.) Events define the notifications for your type. Events
 
 
Search WWH ::




Custom Search