5

I have a system of T4 templates — executed at run-time, not at compile-time — which generate the skeletons of many classes in my app. All these templates reside in a generator tool which is used from time to time to pre-generate new classes into the destination app. The tool contains a configuration class whose properties parameterize the output of all T4 templates.

Originally the configuration class was a static class. However, as the class generator tool grows, it's better to make it non-static and rather make a new instance for each use. The problem is how to pass this instance to the instances of T4 templates. The natural way seems to be inheriting them from a common base which would be provisioned with the configuration class instance.

The problem is that the TextTransformation class, which would have been inherited by my custom T4 base class, is located in an assembly which (according to sources like this SO question) doesn't ship with Visual Studio 2010. Instead it's provided within the Visual Studio 2010 SDK.

The base class generated by VS2010, although itself having no ancestor, isn't partial so there's no way to 'inject' the custom ancestor via another partial declaration.

Thus the question is: Is there a way to inherit a run-time-executed T4 template from a custom base class without the need to install Visual Studio 2010 SDK?

Disclaimer: I'm not very familiar with T4, so I might be completely wrong about how to approach the problem. Hence any other architectural advice is welcome, although my aim isn't to create a super-architected generator — it's just a helper tool, which shall be simple and understandable to the occasional reader.

Community
  • 1
  • 1
Ondrej Tucny
  • 27,626
  • 6
  • 70
  • 90

2 Answers2

7

The solution is actually very simple — the base class can be implemented as a T4 template itself and inherited. Like this:

Base template:

<#@ template language="C#" #>
<#+ 

    public Config Config { get; set; }

#>

Inherited template:

<#@ template language="C#" inherits="BaseTemplate" #>
...
<#= Config.SomeProperty #>
...

Once the template is instantiated the properties declared in the base template are provisioned to the actual configuration.

Ondrej Tucny
  • 27,626
  • 6
  • 70
  • 90
1

The assembly containing the base class does actually ship with Visual Studio, but it is installed in the GAC with no reference assembly. The Visual Studio SDK just contains the referencable version of the DLL.

Consequently if you're creating a new base class in code, you need the VS SDK to build that base class, but not to use it. At rutime, the version in the GAC will be picked up.

GarethJ
  • 6,496
  • 32
  • 42
  • Thanks for clarifying this aspect. I'm accepting your answer as the correct one — however, for my case the solution with a T4 base template I've proposed is more viable. The reason is everyone on my team compiles the generator tool in our main solution, but I don't want VS SDK installed everywhere just for the sake of a few T4 templates used occasionally. – Ondrej Tucny Jan 21 '11 at 22:17
  • I understand your use case - we're hoping to make such a separate install unnecessary in a future release. – GarethJ Jan 22 '11 at 01:40