Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

T4 - TT - Using custom classes in TT files

I would like to use my own class define in a CS file in my TT.

Example:

public class ClassDefinition
{
    public string NameSpace { get; set; }
    public string Name { get; set; }
    public string Protection { get; set; }

    List<ClassProperty> Properties { get; set; }
}

My TT looks like:

<#@ template debug="true" hostspecific="true" language="C#" #>
<#@ output extension=".cs" #>

<#@ assembly name="System" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="System.Xml"#>

<#@ import namespace="System.Xml" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.IO" #>

<#@ include file="$(ProjectDir)ClassDefinition.cs" #>

<#

// Read the model file
XmlDocument doc = new System.Xml.XmlDocument();
doc.Load(this.Host.ResolvePath("GeneratedXmlFile.xml"));

IList<XmlNode> nodeList = new List<XmlNode>();
foreach (XmlNode node in doc.DocumentElement)
{
    switch(node.Name)
    {
        case "Model": 
        {
            ClassDefinition classDefinition = new ClassDefinition();

But I have this error message:

Compiling transformation: The type or namespace name 'ClassDefinition' could not be found (are you missing a using directive or an assembly reference?)

I checked on internet and tried to: - use include - use assembly - use USING But nothing works.

Any ideas ?

like image 639
Eagle Avatar asked Jul 25 '14 14:07

Eagle


People also ask

What is TT file in EDMX?

The ". tt" extension indicates a T4 template file. In this case the template file is responsible for generating the classes that are represented by the Entity Model defined in your edmx file. The edmx. cs file is where the classes generated by the T4 template are put.

What is a .TT file?

TT stands for - Visual Studio Text Template is a software development tool created by the Microsoft. Further explanation - TT file contains text block and control logic used for generating new files. To write the Text Template file we can use either - Visual C# or Visual Basic Code.


1 Answers

If I understand you correctly, you're trying to reuse a class as part of your template generation.

That class needs to be in a tt file itself, build action is set to none, custom tool - nothing. What I have is a template manager class with the following at the top:

<#@ template language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Diagnostics" #>

<#+
public class TemplateManager
{

Then in the other t4 templates I use:

<#@ include file="TemplateManager.tt"#>

and then

List<Values> values = TemplateManager.PrepareVariables(code, container, itemCollection.OfType<EntityType>())

In your case that ClassDefinition.tt file would contain:

<#@ template language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Diagnostics" #>

<#+
public class ClassDefinition
{
    public string NameSpace { get; set; }
    public string Name { get; set; }
    public string Protection { get; set; }

    List<ClassProperty> Properties { get; set; }
}
#>

Then you can include

<#@ include file="ClassDefinition.tt"#>
like image 128
reckface Avatar answered Sep 27 '22 23:09

reckface