Prev | Current Page 360 | Next

Jon Skeet

"C# in Depth: What you need to master C# 2 and 3"


Alternatively, the generated code can provide a base class with virtual methods that do
nothing by default. The manually generated code can then derive from the class and
override some or all of the methods.
All of these solutions are somewhat messy. C# 3??™s partial methods effectively provide
optional hooks that have no cost whatsoever if they??™re not implemented??”any calls
to the unimplemented partial methods are removed by the compiler. It??™s easiest to
understand this with an example. Listing 7.2 shows a partial type specified in two files,
with the constructor in the automatically generated code calling two partial methods,
one of which is implemented in the manually generated code.
GuiPage.xaml.cs
(Handwritten C#)
GuiPage.xaml
(XAML)
GuiPage.g.cs
(C#)
GuiPage type
(Part of an assembly)
XAML to C#
converter
(Build time)
Customer.cs
(Handwritten C#)
Schema/model
(Database, XML, etc)
GeneratedEntities.cs
(C# - includes partial
Customer class)
Customer type
(Part of an assembly)
Code generator
(Prebuild)
Using XAML for declarative UI design Prebuilding partial classes for database entities
C# compilation
C# compilation
Figure 7.2 Comparison between XAML precompilation and autogenerated entity classes
189 Partial types
// Generated.cs
using System;
partial class PartialMethodDemo
{
public PartialMethodDemo()
{
OnConstructorStart();
Console.


Pages:
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372