Search code examples
c#t4envdte

EnvDTE types are not recognized in T4 template


I am trying to get up to speed with T4 templates. I found the following example (here):

<#@ template hostspecific="True" #>
<#@ output extension="txt" #>
<#@ include file="T4Toolbox.tt" #>
<#@ import namespace="EnvDTE" #>
<#
  CodeEnum enumeration = GetEnum("ContactType.cs");
  WriteLine("Found enumeration " + enumeration.Name);
  foreach (CodeElement element in enumeration.Children)
  {
    CodeVariable value = element as CodeVariable;
    if (value != null)
      WriteLine("… found value " + value.Name);
  }
#>
<#+
  private CodeEnum GetEnum(string enumFile)
  {
    ProjectItem projectItem = TransformationContext.FindProjectItem(enumFile);
    FileCodeModel codeModel = projectItem.FileCodeModel;
    return FindEnum(codeModel.CodeElements);
  }

  private CodeEnum FindEnum(CodeElements elements)
  {
    foreach (CodeElement element in elements)
    {
      CodeEnum enumeration = element as CodeEnum;
      if (enumeration != null)
        return enumeration;
      enumeration = FindEnum(element.Children);
      if (enumeration != null)
        return enumeration;
    }
    return null;
  }
#>

Somehow none of the types that are in the EnvDTE namespace are recognized. I am using the Visual T4 extension. All EnvDTE types are underlined in red. The template doesn't run, and I'm getting a list of errors like:

The type or namespace ... could not be found (are you missing a using directive or assembly reference?)

Does anyone know how to solve this?


Solution

  • Have you added a reference to ENVDTE and ENVDTE80 (90, etc) to your project?