Recipe6.11.Generating Code That Is No Longer in Your Main Code Paths


Recipe 6.11. Generating Code That Is No Longer in Your Main Code Paths

Problem

Occasionally as a developer you run into a situation in which it would be handy to be able to regenerate your class based on a set of data that can change. You need to be able to do this without destroying all of the logic you have already created or causing yourself a painful merge between an old and a new class file.

Solution

Write a utility that can regenerate the code that is dependent on external data and keep the generated code in a separate file that defines a partial class. To demonstrate this, we have created a Visual Studio 2005 add-in called PartialClassAddin in the sample code that will allow you to enter a class name and then select which attributes to apply to the class. This is a standard add-in generated by selecting the add-in template from the project wizard. Its main dialog box is shown in Figure 6-1.

Figure 6-1. Attributed Class Wizard from partial class add-in


Enter a class name MyNewClass, select the System.CLSCompliantAttribute and the System.SerializeableAttribute from the list, and click the OK button. This generates the MyNewClass_Attributes.cs file with the following in it:

 // Using directives using System; namespace NamespaceForMyNewClass {     #region Attributes     [System.CLSCompliant(true)]     [System.Serializable()]     #endregion // Attributes     public partial class MyNewClass     {         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 building 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 the Visual Studio 2005 add-in wizard, and the project has the form added to it. The form loads all types derived from System.Attribute to populate the listbox, then uses reflection to figure out the parameters. Once the code has been built, run the project from the debugger. When VS2005 comes up, you can access the Tools menu and the PartialClassAddin menu item to get to this wizard. You can unregister this add-in by going to the Tools menu in VS2005 and selecting the Add-In Manager option. The Add-In Manager dialog is shown in Figure 6-2.

Figure 6-2. Visual Studio 2005 Add-In Manager


Uncheck the PartialClassAddin to remove this from your main environment.

See Also

See the "Partial Class Definitions," "Creating Automation Objects," and "Attribute" topics in the MSDN documentation.



C# Cookbook
Secure Programming Cookbook for C and C++: Recipes for Cryptography, Authentication, Input Validation & More
ISBN: 0596003943
EAN: 2147483647
Year: 2004
Pages: 424

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net