
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
362
|
Chapter 6: Iterators and Partial Types
public MyNewClass( )
{
}
}
}
By making MyNewClass a partial class, you can add this generated file to your project
and replace it when the class attributes need to be updated, while you store your main
logic in another file (perhaps MyNewClass.cs) with a partial
MyNewClass definition:
// Using directives
using System;
using System.Diagnostics;
namespace NamespaceForMyNewClass
{
public partial class MyNewClass : BaseClass
{
public DoSomeWork ( )
{
for(int i=0;i<10;i++)
{
Debug.WriteLine(i);
}
}
}
// Declare base class...
public BaseClass
{
public BaseClass( )
{
}
}
}
Notice that in the file you hold the logic in (MyNewClass.cs as shown before), the
class can declare its inheritance from
BaseClass as well as define some functionality
(
DoSomeWork method).
Discussion
Generating code is not something to do lightly. But in certain circumstances build-
ing a tool can save you a lot of time over the course of maintaining a project. Partial
classes provide a nice way to separate your mainstream code from the “noise” that
changes only in response to external pieces. Windows Forms and Windows Forms
controls are both now declared as partial, as are the
DataSets generated from an XSD
schema to help facilitate the generated code model.
This add-in was created using ...