T4 – Avoid getting compilable output

Say you have a handy parameter driven T4 template to create a business object BusinessObject.tt. You then have one T4-file per business object Customer.tt, User.tt, Role.tt. Now when editing BusinessObject.tt it outputs a .cs file with a class name like MyClass. This output file will naturally not compile since MyClass isn’t part of your domain.

A simple solution is to set <#@ output extension=”.txt” #> in BusinessObject.tt but keep <#@ output extension=”.cs” #> in Customer.tt, User.tt etc.
A drawback is that the output from BusinessObject.tt won’t compile so you won’t get feedback from the compiler. My workaround for this is to save Customer.tt once in a while to check for compilation errors.

Here is another way.

Leave a Reply