Welcome to CollectionGen!

CollectionGen is a Custom Tool Add-In to VS.NET to generate type-safe collections. As it turns out, I did almost none of the work. Jon Flanders figured out how to add a custom tool. Shawn Van Ness implemented the template for type-safe collections. I just put it together.

CollectionGen is an add-on to generate code for type-safe collections until we have templates in C# (likely) and VB (unlikely). The benefit of a type-safe collection, of course, is that you can use it without having to cast items to and from objects. Also, Shawn has been very careful to implement a collection class that is very efficient for both reference types and value types.

Once you've setup it up and defined your collections in a collection definition file in your project, you'll have type-safe collection classes generated as part of your design-process, as shown here:

Figure 1: collections.xml collection definition file

 

Figure 2: collections.xml and generated collection.cs implementation file

 

Figure 3: CollectionGen custom tool add-in associated with the collections.xml file

 

Figure 4: Generate type-safe collection code

Setup

To setup the CollectionGen custom tool, run the setup.bat file in the redist directory (making sure that regasm.exe is on the PATH). It will export the types in SBCollectionGenerator.dll to COM and add the appropriate Registry entries for this custom tool.

Usage

To generate type-safe collections using the CollectionGen custom tool add-in, you need an XML file of the following format:

<typeSafeCollections>

<typeSafeCollection>

<templateKind>Vector</templateKind>

<itemType>int</itemType>

<collectionName>IntegerVector</collectionName>

<collectionNamespace>MyCollections</collectionNamespace>

</typeSafeCollection>

<typeSafeCollection>

<templateKind>HashTable</templateKind>

<keyType>string</keyType>

<itemType>System.Drawing.Point</itemType>

<collectionName>StringPointHashTable</collectionName>

<collectionNamespace>MyCollections</collectionNamespace>

</typeSafeCollection>

 

<typeSafeCollection>

<templateKind>SortedList</templateKind>

<keyType>string</keyType>

<itemType>int</itemType>

<collectionName>StringIntSortedList</collectionName>

<collectionNamespace>MyCollections</collectionNamespace>

</typeSafeCollection>

</typeSafeCollections>

Notice that I'm generating three type-safe collections here:

  1. MyCollections.IntegerVector, which is a vector-style collection holding items of type integer.
  2. MyCollections.StringPointHashTable, which is a hash table holding items of type System.Drawing.Point, hashed on keys of type string.
  3. MyCollections.StringIntSortedList, which is a sorted list of string keys with associated integer values.

As you can see, you can have a single collection definition file with multiple type-safe collection definitions or you can have multiple collection definition files (or a mixture of both, really). I imagine it's slightly easier to have a single file with multiple collections right now, since you need to associate the collection definition file with the custom tool manually.

Once you've created your XML file defining the collection types to be generated, add the file to your project and set the Custom Tool property to "SBCollectionGenerator" (no quotes). After you've done that, and VS.NET notices, you'll get an associated .cs or .vb file. To see it, make sure that Show All Files is selected at the top of the Solution Explorer. From then on, whenever you change the contents of your collection definition file, the code will be regenerated. When you build, it will be part of your project.

What's a Custom Tool?

A custom tool is a code-generator integrated into VS.NET. For example, when you add a dataset .xsd file, the Custom Tool property is set on the file to MSDataSetGenerator. This key maps to a Registry entry kept under HKLM\SOFTWARE\Microsoft\VisualStudio\7.0\Generators\{LangGuid}\MSDataSetGenerator, where {LangGuid} is {FAE04EC1-301F-11D3-BF4B-00C04F79EFBC} for C# and {164B10B9-B200-11D0-8C61-00A0C91E29D5} for VB.

The Registry entry lists the friend name (as the default value) and CLSID and GeneratesDesignTimeSource named values, the latter a flag as to whether the code is generated at design-time or not and the former a COM CLSID to the implementation of the custom tool add-in. See CollectionGen.reg for the details of these Registry entries.

The COM CLSID is an implementation of two COM interfaces, IObjectWithSite and IVsSingleFileGenerator. However, before you get too far into importing these interfaces into .NET, Microsoft has provided a public base class implementation of these two interfaces called BaseCodeGeneratorWithSite (defined in the vs.net\Common7\IDE\Microsoft.VSDesigner.dll assembly). Implementing a custom tool is as easy as deriving from BaseCodeGeneratorWithSite, marking the class with a Guid attribute (representing the COM CLSID) and implementing GenerateCode:

public virtual byte[] GenerateCode(string fileName, string fileContents);

The first argument is the file associated with your tool in VS.NET via the file properties. The second argument is the contents of this file read into a string for your convenience. The return value is a set of bytes that will be written to the file associated with the tool file in VS.NET. For example, foo.xsd associated with the MSDataSetGenerator custom tool will cause the IDE to create a foo.cs file and compile it with the project.

The code generation process itself happens when the file associated with the custom tool is saved, thereby making all of the generated types and the related Intellisense available as part of the design-time experience.

History

Limitations

License

Copyright © 2002-2003 Chris Sells

This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software.

Permission is granted to anyone to use this software for any purpose, including commercial applications, subject to the following restrictions:

  1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation is requested, as shown here:

    Portions copyright © 2002-2003 Chris Sells (http://www.sellsbrothers.com/).
     
  2. No substantial portion of this source code may be redistributed without the express written permission of the copyright holders, where "substantial" is defined as enough code to be recognizably from this code.

Permission

The following folks have permission to redistribute CollectionGen in source code form: