Run t4 script as custom tool for resx file

2019-07-11 04:29发布

问题:

I have a resource file MyResource.resx, and I want to change how the MyResource.Designer.cs file gets generated.

I have a t4 script that accepts a resx file as input and gives me the resulting transform, however, I must manually run this t4 in order for it to work.

I see that the "custom tool" property for the resx file is currently specified as PublicResXFileCodeGenerator, I changed it to TextTemplatingFileGenerator, but that only made Visual Studio execute the resx as if it were a t4.

How do I configure my t4 to automatically run on my resource when the resource is changed?

回答1:

Check this out: AutoRunCustomTool or Download from Visual Studio Gallery

From the readme:

After you install the extension, you should see a new Run custom tool on property on each project item. Just edit this property to add the name(s) of the target file(s). That's it!

"target" files are your .tt files



回答2:

There is no good way to have Visual Studio automatically transform templates whenever a resource changes, but MSDN does call out a setting you can add in your project file to transform on build:

https://msdn.microsoft.com/en-us/library/dd820620.aspx#Regenerating