T4 Compiling transformation: The type or namespace name 'Template' could not be found

404Dreamer_ML picture 404Dreamer_ML · Aug 22, 2011 · Viewed 9.4k times · Source

I am trying to generate a Xaml file using Template T4 and defining a reusable file Xaml.tt like this

<#@ template language="C#" hostspecific="True" debug="True" #>    
<#@ assembly name="System.Xml.dll" #>
<#@ assembly name="System.Xml.Linq.dll" #>
<#@ import namespace="System.Xml" #>
<#@ import namespace="System.Xml.Linq" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Collections" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="Microsoft.VisualStudio.TextTemplating" #>
<#+
public class Xaml : Template
{
    public override string TransformText()
    {
#> 
        // Xaml Content

  <#+
    return this.GenerationEnvironment.ToString();   }

    }
   #>

But i have an error: the template namespace couldn't be found, any one has idea about that ? Still same error !! frustrating !!

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

I am getting the same error for Generator.

I created these file by adding new item Code Generation > Template

How to create the Xaml file

The default structure of the file is and still not recognizing Template.

<#+
// <copyright file="Template1.tt" company="HP">
//  Copyright © HP. All Rights Reserved.
// </copyright>

public class Template1 : Template
{
public override string TransformText()
{

    return this.GenerationEnvironment.ToString();
}
}
#>

Is the an assembly reference or a directive i am missing ?

BTW i am using T4 Toolbox also

Answer

Baz1nga picture Baz1nga · Aug 22, 2011

you need to add a bunch of meta tags for that.. the ones that I usually add are as follows:

<#@ template language="C#v3.5" hostspecific="True" debug="True" #>
<#@ output extension=".cs" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="System.Xml" #>
<#@ import namespace="System.Xml" #>
<#@ import namespace="System.Text" #> 
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Collections.Generic" #>

what is Template here referring to.. that is the class whose namespace it is not able to find I think.. Why are you extending that? if you need it give a fully qualified namespace for the same..