I have made a simple design-time template that uses an enum that resides in the same project. The template cranks out a class definition for each variable in that enum type.
I noticed that changing and saving the .tt file in anyway makes it crank out the c# code again. I am hoping that I can set the solution up so that if I change the enum and then save, then the template might also crank out the source again.. That way when I want to add a new type, I could add a variable to the enum definition and maybe just press save.
At the moment I have to do this:
- Add variable to the enum definition in its .cs file.
- Re-compile the project.
- Go “run custom tool” on the template .tt file.
- then need to compile again to compile-in the t4-generated source.
So, maybe there is a way to get the template to “detect” a change in another source with that it uses,
and to act like it has been modified itself?
Start by installing T4 Toolbox. It adds numerous features that makes working with T4 files much easier.
Change the Custom Tool property of your .cs file to
T4ScriptFileGenerator. This will create a new .tt file underneath the .cs file. (Have a look at this blog post from Oleg Sych, the author of T4 toolbox, for more information). Now, anytime you save the original .cs file, T4 Toolbox will run the matching .tt file.In this new file, you can add the code that read the enum. However, it seems you’re using reflection on the generated assembly, which requires rebuilding the project each time you make a change. Use Visual Studio
EnvDTEinstead to read yourenum. You have access to the abstract syntax tree of parsed C# files in the IDE without the need for them to be compiled.