This project is read-only.

T4 inside a Visual Studio 2010 Add-In

Feb 16, 2011 at 2:29 AM

I'm trying to process a template from within a Visual Studio 2010 Add-In(C#) that I've created, but am getting the following exception:

An Exception was thrown while running the transformation code. The process cannot continue. The following Exception was thrown:
System.NullReferenceException: Object reference not set to an instance of an object.
at T4Toolbox.DteProcessor.CreateLogIfNecessary(ICollection`1 outputFiles, ProjectItem template)
at T4Toolbox.DteProcessor.UpdateFiles(ICollection`1 outputFiles)
at T4Toolbox.OutputManager.UpdateFiles()
at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
at T4Toolbox.TransformationContext.OnTransformationEnded(TextTransformation transformation)
at Microsoft.VisualStudio.TextTemplatingF9F58D9FDAFF6D9738B360BD046E8218.GeneratedTextTransformation.Dispose(Boolean disposing)
at Microsoft.VisualStudio.TextTemplating.TextTransformation.Dispose()
at Microsoft.VisualStudio.TextTemplating.TransformationRunner.RunTransformation(TemplateProcessingSession session, String source, ITextTemplatingEngineHost host, String& result)

I'm using the following technique from within the Add-In to try and get this to work:

TextReader tr = new StreamReader(@c:\"");
string content = tr.ReadToEnd();
IServiceProvider serviceProvider = new ServiceProvider((Microsoft.VisualStudio.OLE.Interop.IServiceProvider)m_applicationObject.DTE);
ITextTemplating tt = serviceProvider.GetService(typeof(STextTemplating)) as ITextTemplating;
tt.ProcessTemplate(contextTTFile, content, this, null);

I'm able to successfully run the template using Run Custom Tool from within a Solution, but for automation purposes need to get it running from the Add-In. Any ideas what I might be doing wrong that would cause this exception?

Feb 16, 2011 at 9:52 PM

In trying to isolate the problem I found that the include causes the exception.  I'm unable to process a simple template as follows:

<#@ template language="C#" debug="false" hostspecific="true"#><#@ output extension="generated.cs"#>
<#@ include file="" #>
// "Test"

If I remove the, the exception goes away.  As I mentioned in the first post I'm in the context of Visual Studio as an addin, so I'd expect this to work.  Are there some other environment settings that must be set so that the toolkit won't assert?  What is it trying to initialize?  Any help would be greatly appreciated.


Feb 27, 2011 at 9:17 AM

Just to add myself to the queue - I've found myself into exactly the same situation and l'm stuck ...

Moreover, when I'm processing a .tt template which includes from within a vs2010 add-in using VS host (just as described above) I have to specify the full path to where the T4 toolbox is installed (C:\Program Files\...). This is pretty weird, it seems that the VS host ignores the registry keys HKLM\Software\Microsoft\Visual Studio\10.0\Text Templating\IncludeFolders\.tt. 


Aug 17, 2011 at 11:39 PM

Yes, I'm having the same exact issue.  I've included in my template and I get the exact same exception when I try to run the custom tool from VS2010 Solution Explorer.  Any update?


Aug 19, 2011 at 9:35 AM


I have never tested this. The fastest way to solve this is dig in with a debugger, I simply don't have the cycles to spend on this right now.