How to get Visual Studio 2008 to regenerate code from T4 templates when modifying an XML file?

I generate quite a lot of code from a single XML file, but the templates are organized into two different T4 templates. Whenever I change an XML file, I have to remember that I need to open two files *.tt, change them trivially (add / remove a space) and save them again to make sure that the code is generated.

This may not be the right way!

Ideally, I would like Visual Studio 2008 to perform text conversion in T4 files if the XML file was modified. I lost a little, since I really do not know how Visual Studio builds projects in C #, so pointers in this direction will also help (I could try to figure it out myself).

+5
source share
2 answers

You can use the T4ScriptFileGenerator from T4Toolbox as a custom tool for your XML file. Say you have a Test.XML file in your project with this custom tool. The first time you save Test.XML, this custom tool creates a new Test.tt file. You can place the code generation logic there (in place or # include other .tt files). The next time you save Test.XML, it will convert (generate code) the existing Test.tt.

+2
source

, , , . , , XAML #.

, A.tt B.tt, C.xml, C.xml.

. A.tt B.tt :

<ItemGroup>
    <None Include="A.tt">
      <Generator>TextTemplatingFileGenerator</Generator>
      <LastGenOutput>A.cs</LastGenOutput>
      <DependentUpon>C.xml</DependentUpon>
    </None>
    <None Include="B.tt">
      <Generator>TextTemplatingFileGenerator</Generator>
      <LastGenOutput>B.cs</LastGenOutput>
      <DependentUpon>C.xml</DependentUpon>
    </None>
</ItemGroup>

, (...)

<ItemGroup>
    <None Include="C.xml" />
</ItemGroup>

A.cs B.cs:

<Compile Include="A.cs">
      <DependentUpon>A.tt</DependentUpon>
      <AutoGen>True</AutoGen>
      <DesignTime>True</DesignTime>
</Compile>
<Compile Include="B.cs">
      <DependentUpon>B.tt</DependentUpon>
      <AutoGen>True</AutoGen>
      <DesignTime>True</DesignTime>
</Compile>

, , , , <DependentUpon>C.xml</DependentUpon> ItemGroup A.tt B.tt.

+2

All Articles