One feature that was almost immediately requested was a way to provide optional
???extra??? code for generated methods to call. This need has been addressed by C# 3 with
partial methods.
188 CHAPTER 7 Concluding C# 2: the final features
7.1.3 Partial methods??”C# 3 only!
Just to reiterate my previous explanation, I realize that the rest of this part of the book
has just been dealing with C# 2 features??”but partial methods don??™t fit with any of the
other C# 3 features and they do fit in very well when describing partial types. Apologies
for any confusion this may cause.
Back to the feature: sometimes we want to be able to specify behavior in a manually
created file and use that behavior from an automatically generated file. For instance,
in a class that has lots of automatically generated properties, we might want to be able
to specify code to be executed as validation of a new value for some of those properties.
Another common scenario is for a code-generation tool to include constructors
??”manually written code often wants to hook into object construction to set
default values, perform some logging, and so forth.
In C# 2, these requirements could only be met either by using events that the manually
generated code could subscribe to, or by making the automatically generated
code assume that the handwritten code will include methods of a particular name??”
making the whole code fail to compile unless the relevant methods are provided.
Pages:
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371