System.Collections.ObjectModel.KeyedCollection<T> CodeSmith Template

downloads, dotnet comments edit

I’ve been converting a .NET 1.1 project over to .NET 2.0 and there are a lot of strongly-typed collections in the project that were generated by CodeSmith.  Most of these collections were based on the ArrayList template that came bundled with CodeSmith and all of them used the “GetByKey” facility the generator offered.

I’m trying to reduce the codebase and take advantage of generics where possible to get rid of things like this.  To that end, I’ve been converting most of these over to System.Collections.ObjectModel.KeyedCollection<T> (which also jives nicely with FxCop - the generated collections came bundled with several items that set FxCop off).

Ironically, that means I end up generating a very small amount of code (and corresponding XML documentation) for each of these collections - a class derived from KeyedCollection with the appropriate constructor overloads and the override for the GetKeyForItem method.  So I made a - wait for it - CodeSmith template to do exactly that.

If you’re a CodeSmith user and you find yourself creating a lot of simple KeyedCollection implementations, grab the template and go to it.

Comments