After reading an article on CodeProject entitled VS.NET CodeDOM-Based Custom Tool for String Resource Management by Jasmin Muharemovic, and finding the StringResourceTool custom tool for string resources, I discovered how easy it is to create a custom tool for Visual Studio .NET.
In order to generate code, you use the CodeDOM objects, which appear to be fairly complex and cumbersome to code. Also, if the "source" file for the code generation is in some text format, then a whole lot of string parsing needs to be performed to extract the "parameters" required to generate the appropriate code.
Another issue is that, each time you want to generate something else, you have to create another custom tool and register it in Visual Studio. This could lead to a "DLL hell" of custom tools, as well as trying to remember each one's name would be difficult.
The Solution: XML + XSLT
XML takes all this hard string parsing stuff away, and provides a powerful mechanism (XPath) for extracting whatever you want out of the XML file. XSLT provides a flexible mechanism for producing output in any format. Thus, I adapted the custom tool code to run an XSL transform instead of producing the generated code.
In this way, each time I want to create a new type of code generator, I just invent a new XML schema for the source file and a corresponding XSL transform to generate the required code.
How it Works
There are two requirements for the
TransformCodeGenerator to work properly:
- The XML source files root element must contain an attribute called "
transformer". The value of this attribute specifies which XSLT file to use for the transformation.
- The XSLT filename may be fully qualified, or just the file name, in which case, the file must reside in the same directory as the XML file (in the project), or in the same directory as the TransformCodeGenerate.dll files.
Add your XML file to your Visual Studio solution, and set the
CustomTool property to
TransformCodeGenerator. Each time you modify and save the XML file, the custom tool will regenerate your code.
I created two sample XML files to illustrate the possible usage for this custom tool.
I invented an XML schema which describes a WinForms form with some controls on it. Check out the "MyXMLForm.xml" file in the test project. The accompanying XSLT transform, "MakeXMLFormCode.xslt", transforms this into code which creates a class which has fields for each control, creates a new instance of each control, sets the control properties, and adds them to the form.
The second example is for "type-safe"
DataTables. This XML file describes a database table in a more friendly way than an XSD file. This time, the XSLT generates two classes which inherit from
DataRow, respectively. The
DataRow class will have type-safe properties for each column that the
DataTable contains. You could take this sample further by creating a tool which reads a database schema and produces this XML file... :-)
XML + XSLT provides a powerful way to produce code generated code, and now with the
TransformCodeGenerator custom tool for Visual Studio, it's a whole lot easier than running an external tool to run the transform.
Please note: This article does not intend to go into the details of how to author XML files or XSLT transformations.
I'm a developer specializing in software for the financial services industry particularly in the decision support area. Currently my specialty is in investment compliance.