We add a structure of T4 template files (tt and ttinclude ones) in a project in Visual Studio.

For these templates we are using our custom transformation and processing and we do NOT want the Custom Tool setting of the tt files which by default is set to TextTemplatingFileGenerator.

In order to achieve that we are using the following code:

... // logic for the files creation
ProjectItem addedItem = this.project.ProjectItems.AddFromFile(fileFullPath);
addedItem.Properties.Item("CustomTool").Value = string.Empty;

The problem is that when the files are added to the project a code generation is caused by the default custom tool and even after we remove it from the properties of the tt files the errors caused by the transformation are still in the Errors List (its something like visual glitch).

They disappear as soon as you save a file, navigate to other file or build the project but we do not want our users to see these errors and make any additional actions to get rid of them.

We tried to access the errors collection using the DTE and DTE2 classes like

DTE2 dte2 = (DTE2)this.project.DTE;
dte2.ToolWindows.TaskList;

or getting them from the ErrorListProvider provider as

var provider = new ErrorListProvider(this.ServiceProvider);
var tasks = provider.Tasks;

but the TaskList is not containing these errors - may be the reason is that they are transformation errors not a compile ones.

Even more strange is that the DTE.Events.TaskListEvents.TaskAdded event is firing for each of the transformation errors but they cannot be found in the TaskList. Also calling the Delete() method of the errors got from the TaskAdded event is not removing them from the errors list.

We also tried to programmatically navigate through the files or save the tamplates but it is not refreshing the errors list like when doing it manually.

How can we refresh the ErrorList or tell Visual Studio not to add a Custom Tool setting when we add the templates to the Project?

P.S. We do not want to force a build of the project of the clients (it is removing the errors but it is not applicable for us).

有帮助吗?

解决方案

We found a dirty workaround to get rid of the errors.

We generate the template files with their basic template content (the one below) which is valid for the default TextTemplatingFileGenerator and the initial code generation is not causing any errors.

<#@ template  debug="true" hostSpecific="true" #>
<#@ output extension=".cs" #>
<#@ Assembly Name="System.Core" #>
<#@ Assembly Name="System.Windows.Forms" #>
<#@ import namespace="System" #>
<#
    // This is a temporary content valid for the TextTemplatingFileGenerator.
#>

Then after removing the custom tool setting we replace the content with the real one and everything is working as expected because the files are already in the Project and there are not any other transformations without a custom tool.

Not the best solution but we were not able to find better. I hope this helps.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top