0

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.

GregRos
  • 8,667
  • 3
  • 37
  • 63

3 Answers3

1

You could theorically use T4 templating in Visual Studio to generate such code with templates assigned to each $ClassName$.

Konrad Kokosa
  • 16,563
  • 2
  • 36
  • 58
1

Have you considered using T4 Text Templates?

Michael Gunter
  • 12,528
  • 1
  • 24
  • 58
  • Thank you, that seems to be exactly what I'm looking for. However, how do I generate "multiple instances" of a single template, each parametrized differently? – GregRos Dec 04 '13 at 20:26
1

You can make it as following.

.1. Parametrize the T4

Add parametrization in the generation - with traditional class variables or longer block templates. Files that it reads during the processing based on some parameter values and such.

.2. Take control of the generation, regardless if it's precompiled or instantly run

I'll copy paste part of how I've approached the external control of code generation

    public Tuple<string, string>[] GetGeneratorContent(params string[] xmlFileNames)
{
    List<Tuple<string, string>> result = new List<Tuple<string, string>>();
    foreach(string xmlFileName in xmlFileNames)
    {
        TheBallCoreAbstractionType abs = LoadXml<TheBallCoreAbstractionType>(xmlFileName);
        CurrentAbstraction = abs;
        this.GenerationEnvironment.Clear();
        string content = TransformText();
        string outputFile = Path.GetFileNameWithoutExtension(xmlFileName) + ".designer.cs";
        result.Add(Tuple.Create(outputFile, content));
    }
    return result.ToArray();
}

The notable things in above is TransformText() that runs the template "as normally", and before that "this.GenerationEnvironment.Clear()". Around that some class variables are set and then the output is returned as filename + content - that could of course be just File.WriteAllText(name, content);.

For further interest - to "parametrize on massive scale" I've added more below...

Disclaimer I've worked extensively on completely open approach to modularize T4 automation. I'll link few links around this, as while the answer is somewhat tightly packed already above, the various ways of T4 controlling (especially on original question's case for parametrization management) is quite interesting and extremely powerful art.

That being said - here's few full-ground-up links that used VS2010 and T4 Toolbox, but can be ran without the T4 Toolbox on latter studios by clicking "Transform All Templates": https://github.com/kallex/MSTechDays2012Demos

The T4 demos are from ground up, the ADM part is for abstraction control achieved through T4.

The approach was recognized quite a while ago with positive feedback from T4 team :-): http://blogs.msdn.com/b/t4/archive/2011/11/30/some-nice-new-getting-started-with-t4-videos.aspx

From demo videos there are various demonstrations on what can be achieved, but for pure/advanced T4 I recommend the Getting Started & Advanced T4 ones: http://www.youtube.com/playlist?list=PL6D51E9F1B9C955BB

Kallex
  • 498
  • 2
  • 10