title: T4 Text Transformations in VS2013 and VS2015
body: <p>Our company has legacy system that heavily relies on T4 and the employee who architected it is gone.  It was running fine for us, however recently some developers upgraded to VS2015.  The T4 transformations stopped working for them (with error similar as reported below).  It looked at though there were references to <code>Microsoft.VisualStudio.TextTemplating.12.0.dll</code>.  They changed the references to '14' and all worked for them.  However, the same project shared by other developers on VS2013 no longer worked with error: </p>
  
  <blockquote>
    <p>Compiling transformation: The type
    'Microsoft.VisualStudio.TextTemplating.TextTransformation' is defined
    in an assembly that is not referenced. You must add a reference to
    assembly 'Microsoft.VisualStudio.TextTemplating.14.0,
    Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.</p>
  </blockquote>
  
  <p>Are you able to run T4 on same project that is opened in both VS2013 and VS2015?  One side note, there was a dependent assembly that the old employee made that all the TextTransformations derived from (it also provides helpers that are used in the *.tt files).  Unfortunately it used an interface only present in <code>Microsoft.VisualStudio.TextTemplating.Interfaces.10.0.dll</code>, so that old reference is being dragged aroundÎ.  Not sure if that is contributing or not.  But basically here is the bottom line:</p>
  
  <p>When all devs on VS2013, everything worked and references were:</p>
  
  <pre><code>Microsoft.VisualStudio.TextTemplating.12.0.dll
  Microsoft.VisualStudio.TextTemplating.Interfaces.10.0.dll
  Microsoft.VisualStudio.TextTemplating.VSHost.12.0.dll
  </code></pre>
  
  <p>Then when some went to vs2015, the only way to get it to work was to change out the 12.0* dlls with 14.0, but then vs2013 developers ceased to work.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I might not have clarified our complete setup.  We have 20-30 *.tt files in a separate project that is included in the solution with the project that will have Text Transformations applied to it.</p>
  
  <ol>
  <li><p>We have a helper Extensibility.CodeGeneration.dll (this references Microsoft.VisualStudio.TextTemplating.N.dll files as well) that have several static helpers and also base classes that derive from Microsoft.VisualStudio.TextTemplating.TextTransformation.</p></li>
  <li><p>The 'templates' project that contains all the *.tt files, <em>every</em> <em>.tt file use static methods from our helper dll.  It also references all the same Microsoft.VisualStudio.</em> dlls.</p></li>
  <li><p>In <em>every</em> *.tt file, we have something that looks like this, where AreaTemplate is a class defined in the *.tt file itself and either derives from Microsoft.VisualStudio.TextTemplating.TextTransformation or one of the exposed base classes in our helper dll.  </p>
  
  <p>var template = new AreaTemplate { Settings = settings, Area = area, Layouts = layouts };
  Write( template.TransformText() );</p></li>
  <li><p>It was asked in comments how I obtained/used a 'host'.  In searching through code we have a few common scenarios (all done inside the helper dll).  In each instance, host is of type <code>ITextTemplatingEngineHost</code>.</p></li>
  </ol>
  
  <p>Case 1:  </p>
  
  <pre><code>var dte = (EnvDTE.DTE)( (IServiceProvider)host ).GetService( typeof( EnvDTE.DTE ) );
  </code></pre>
  
  <p>Case 2:  </p>
  
  <pre><code>var hostServiceProvider = (IServiceProvider)host;
  </code></pre>
  
  <p>Given #3 above, I think I <em>have to</em> reference the Microsoft.TextTemplating dlls (and not just the Interfaces dll) due to the use/exposing of TextTransformation class.</p>
  
  <p>Also, if I change references to in both my 'Helper' and 'Templates' project to 12.0...in vs2015, I get error about 'you must add reference to 12.0'...I have references to that in both applicable (in my eyes) projects...not sure why VS tells me to add it.  I tried adding an explicit reference in the *.tt file using </p>
  
  <pre><code>&lt;#@ assembly Name="$(ProjectDir)..\..\Assemblies\Microsoft.VisualStudio.TextTemplating.12.0.dll" #&gt;
  </code></pre>
  
  <p>But then I got the error </p>
  
  <blockquote>
    <p>The type 'Microsoft.VisualStudio.TextTemplating.TextTransformation'
    exists in both
    'c:\BTR\Source\Assemblies\Microsoft.VisualStudio.TextTemplating.12.0.dll'
    and
    'c:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.VisualStudio.TextTemplating.14.0\v4.0_14.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.TextTemplating.14.0.dll'</p>
  </blockquote>
  
  <p>It is almost like there is a hidden/implicit reference to the latest Texttemplating already built into VS?? </p>
  
  <p>Not sure if our setup was a more complicated way to achieve our original goal, but I don't think I'll be able to unwind it and change if we are doing it the incorrect way.</p>
  
  <p>Given our setup let me know if you think I am stuck or not.  I was trying to figure out 'conditions' in MSBuild to help support both Visual Studios but couldn't succeed.</p>
  
  <p>Thanks in advance.</p>