I'm writing a somewhat complicated library in C#, and I need to generate a lot of boilerplate code automatically, from a template. Specifically, I want to achieve something like the following:
public partial class $ClassName$<T>
{
/// ...
/// ...
/// ...
/// (long documentation)
public $ClassName$<TOut> Select(Func<T, TOut> selector, $OtherType$<TOut> other = null)
{
return base.Select(new $ClassName$<TOut>(other), selector);
}
}
(You may notice that this is actually an attempt to emulate higher-kinded type parameters, but that's not particularly important to the question).
However, I don't want the code to be generated in a "static" way, the way the "New Item" wizard can generate it in Visual Studio. This is because the template itself is actually the "code", and it's subject to change. I can't really manually regenerate all the classes using this boilerplate code whenever I change the template.
That's why I'm looking for a way to generate this code during every build, or perhaps on some other action. Since multiple classes will use this boilerplate code (with different template parameters), the code should be generated automatically for every class that uses it. It's also important that the code be available to IntelliSense.
I'm also looking for a tool for writing these templates, and possibly something to manage them somehow.
Any help?
Edit: T4 text templates seem to be pretty much what I'm looking for. However, how would I create "multiple instances" of a template, each with different parameters, and associate each with a different class? The built-in functionality seems to generate only a single file.