Reputation: 31
I have two .tt files (template1.tt & template2.tt) which has some part of the code to be same.
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Reflection" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".cs" #>
<#
Assembly assem1 = Assembly.LoadFrom("a.dll");
Assembly assem2 = Assembly.LoadFrom("b.dll");
#>
The above is the common code within <#...#>
So I wanted to remove the common part from the T4 template. I tried using the creating a base template (Template3).
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Reflection" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".txt" #>
<#+
public void loadAssemblies()
{
Assembly assem1 = Assembly.LoadFrom("a.dll");
Assembly assem2 = Assembly.LoadFrom("b.dll");
}
#>
Now when I am trying to add
<#@ template debug="false" hostspecific="false" language="C#" inherits="Template3" #>
the tt file is not compiling.
The error is
Severity Code Description Project File Line Suppression State Error Compiling transformation: The type or namespace name 'Template3' could not be found (are you missing a using directive or an assembly reference?) Template C:\Users\xxx\source\repos\Template\Template\Template2.tt 1
Any suggestions where I am wrong or how can I remove the duplicates ?
Upvotes: 1
Views: 206
Reputation: 16554
Instead of using inheritance, you should look into .ttinclude
files as explained (its actually not very well explained... merely alluded to) in Guidelines for All T4 Templates. Your .tt
file becomes the main orchestration and you call methods defined in the included files.
Inheritance can work, but it becomes hard to manage long term, you'll find that using include files allow you to be more flexible, by keeping common sets of useful routines in different files, over time you will be able to use these files in a more diverse set of scenarios. Think of
.ttinclude
as being like Header Files in C that bring the other files inline with the current file.
<#@ CleanupBehavior Processor="T4VSHost" CleanupAfterProcessingTemplate="true" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Reflection" #>
<#@ import namespace="System.Collections.Generic" #>
<#+
public static Assembly assem1 = null;
public static Assembly assem2 = null;
public static void loadAssemblies()
{
assem1 = Assembly.LoadFrom("a.dll");
assem2 = Assembly.LoadFrom("b.dll");
}
#>
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Reflection" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ include file="common.ttinclude"#>
<#@ output extension=".cs" #>
<#
loadAssemblies();
#>
Upvotes: 2