T4 template inheritance from custom base class providing Visual Studio 2010 SDK not installed

I have a T4 template system - executed at runtime, not at compile time, which generate skeletons of many classes in my application. All of these templates are in the generator tool, which from time to time is used to pre-create new classes in the target application. The tool contains a configuration class whose properties parameterize the output of all T4 templates.

The configuration class was originally a static class. However, as the class generator tool grows, it is best to make it non-static and rather create a new instance for each use. The problem is how to pass this instance to T4 instances. The natural way, apparently, inherits them from a common base, which will be provided with an instance of the configuration class.

The problem is that the TextTransformation class, which would be inherited by my usual T4 base class, is in an assembly that (according to sources like this SO question ) does not ship with Visual Studio 2010. Instead, it is provided in the Visual Studio 2010 SDK.

The base class generated by VS2010, although it does not have an ancestor, is not partial, so there is no way to "enter" a custom ancestor through another partial declaration.

So the question is: Is there a way to inherit the T4 runtime template from a custom base class without having to install the Visual Studio 2010 SDK?

Disclaimer: I am not very familiar with T4, so I could be completely mistaken in how to approach the problem. Therefore, any other architectural advice is welcome, although my goal is not to create a super-archived generator - it's just an auxiliary tool that should be simple and understandable for the casual reader.

+4
source share
2 answers

The assembly containing the base class does come with Visual Studio, but it is installed in the GAC without a link. The Visual Studio SDK simply contains the referenced version of the DLL.

Therefore, if you create a new base class in code, you need the VS SDK to create this base class, but not to use it. In the routine, the version in the GAC will be raised.

+1
source

The solution is actually very simple - the base class can be implemented as the 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 #> ... 

After the template is created, the properties declared in the base template will be provided in the actual configuration.

+6
source

Source: https://habr.com/ru/post/1335604/


All Articles