Archive for the ‘T4’ Category

exception: Unable to cast object of type ‘System.Windows.Forms.SplitContainer’ to type ‘System.ComponentModel.ISupportInitialize’.

August 18th, 2010

When downgrading a client winform app from Dotnet4 to Dotnet3.5 one might get

Unable to cast object of type 'System.Windows.Forms.SplitContainer' to type 'System.ComponentModel.ISupportInitialize'.

The solution in my case was to just comment out the offending row (BeginInit) and its (EndInit) counterpart.

There is an early bug report on the subject but the error seems to linger.

categories: T4 | 3 comments »

T4 – template and script

July 22nd, 2010

I mentioned earlier a way to write a template to call other templates.  The method worked only for simple solutions.  Now, with VS2010 I recommend following http://www.olegsych.com/2008/09/t4-tutorial-creating-reusable-code-generation-templates/ to create more reusable scripts.

One has to install Oleg’s T4Toolkit to get some new templates.  Mentioned, though hard to parse, is that one must blank the Custom tool property from the “template” template and probably change the output suffix for the “script” template.

TT still has some way to go to get truly simple to use.

categories: T4 | no comments »

T4 – <#@ Assembly name="$(SolutionDir)\T4Helper\bin\debug\T4Helper.dll" #>

July 16th, 2010

As mentioned here and here the assembly referencing has changed from VS2008 to VS2010.

Say you have a T4 helper assembly aptly named T4Helper and you have it in a project called… T4Helper.

MySolution
....MyProject
........MyT4Files
........MyOtherFiles
....T4Helper
........T4Source

The VS-macro-way to reference it from  MyProject is

<#@ Assembly name="$(SolutionDir)\T4Helper\bin\debug\T4Helper.dll" #>

It obviously has problems when switching away from debug compile.  But the line above will help you get the backslashes right at least.

Update:
Something with

$(ProjectDir)$(OutDir)

might do the trick.

categories: T4, tip | no comments »

T4 – how to read output easy

July 15th, 2010

When working with Microsoft’s Text Template Transformation Toolkit there is a lot of ctrl-tabbing to and fro to validate the result.

Instead split the editor  (I am talking Visual studio here) vertically and put the .tt file in one and the output (.cs) file in the other.  Whenever you save the .tt file the output file is updated.  It even remembers the scrolling position.

categories: T4, tip | no comments »

T4 – Template and TemplateTemplate

July 15th, 2010

Update: use this tip instead.

One can call a template from another through <#@ include file=”MyTemplateTemplate.tt” #> Variables defined in MyTemplate.tt are then transfered to MyTemplateTemplate.tt. The trick is to set them in the right order.

Below is also shown a way to avoid unnecessary output. Here is [another].

MyTemplate.tt:

<#
string Greeting = "HelloWorld";
bool createOutput = true;
#>
<#@ include file="MyTemplateTemplate.tt" #>

MyTemplateTemplate.tt:

<#
if( !createOutput )
{
return "//	This file is not in use.";
}
#>

…the template code

<#+
bool createOutput = false;
string Greeting = string.Empty;
#>

As you can see the TemplateTemplate has the variable assignment as a statement “<#+” below/after the code.

Then there is the createOutput flag as a trick to avoid output. Return blank if you want an empty file. Another way to avoid compilable code that won’t compile is here.

categories: T4, tip | no comments »

T4 – Avoid getting compilable output

July 15th, 2010

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.

categories: T4, tip | no comments »

T4 – Getting started

July 15th, 2010

Download the templates from Tangible, there is a gratis version.
Use the Simple and the Advanced templates from Tangible.
Read Oleg’s article.

You might notice that T4 once in a while complains about a right parenthesis. This is often a false error. In some cases you get more errors so just skip to the right-parenthesis-complaint and they will vanish. Other cases this is the only error. Just type a character and delete it and save again. It can even be enough to just step away from the edited row.
The error is always on the same row as the cursor is positioned.

If you know that the output should compile but the compiler complains every time you save the .tt file it is working as it should. Saving the .tt file runs the template transformation but doesn’t start any compilation. So just compile after saving the .tt file and all should be well.

categories: T4, tip | no comments »

T4 – A namespace cannot directly contain members such as fields or methods

July 15th, 2010

If you are using T4 and get this error you are probably having an error in the template which makes it fail transforming.  Check here for a tidbit more information.

An error example:
Error 11 A namespace cannot directly contain members such as fields or methods C:\MyProject\MyOutputFile.cs 1 1 MyProjectName

categories: T4, tip | no comments »