2010/01/14

Codemash: T4 Code Generation with Visual Studio 2008

Presented by Steve Andrews.

  • What is it?
    • Was originally a DSL for creating code based on your model.
  • Demo
    • Need a template directive
      <#@ template language=”C#v3.5” #>
    • Output directive
      <#@ output extension=”csv” #>
      one,two,thee
      four,five,six
    • Expression blocks
      <#= DateTime.Now.ToString() #>
    • Statement blocks
      <# for (int i = 1; i < 6; i++) { #>
      <# } #>
    • Class feature blocks
      <# HelloWorld(); #>
      <#+
      private void HelloWorld()
      {
      }
      #>
    • Include directive
      <#@ include file=”_IncludedFile.tt” #>

      remove the custom tool for the included file if you don’t want it to run.
    • Assembly include directive
      <#@ assembly name=”System.Xml” #>
      <#= new System.Xml.XmlDocument().ToString() #>
    • Import directive
      <#@ assembly name=”System.Xml” #>
      <#@ import namespace=”System.Xml” #>
      <#= new XmlDocument().ToString() #>
  • T4 Toolbox
    • Can generate multiple outputs per template
    • Since the generator has to create a file by default with the same name, use it as a log of the output.
    • Can create classes within the template that can extend Generator
    • Can extend templates as well.
  • Custom Directive Processor
    • Need to put create some registry keys for the custom directives
  • Custom host for running T4.
  • Debugging T4
    • Registry setting that you have to change from 10 to 2 if using above .NET 3.0

Have to say that I thought I knew quite a bit about T4, but really a great presentation overall.

Post a Comment