5

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:

  1. Add variable to the enum definition in its .cs file.
  2. Re-compile the project.
  3. Go "run custom tool" on the template .tt file.
  4. 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?

4

1 に答える 1

3
  1. T4 Toolboxをインストールすることから始めます。T4 ファイルの操作をより簡単にする多数の機能が追加されています。

  2. .cs ファイルのカスタム ツール プロパティを に変更しますT4ScriptFileGenerator。これにより、.cs ファイルの下に新しい .tt ファイルが作成されます。(詳細については、T4 ツールボックスの作成者であるOleg Sychのこのブログ投稿を参照してください)。これで、元の .cs ファイルを保存するたびに、T4 Toolbox は一致する .tt ファイルを実行します。

  3. この新しいファイルに、列挙型を読み取るコードを追加できます。ただし、生成されたアセンブリでリフレクションを使用しているようです。これには、変更を加えるたびにプロジェクトを再構築する必要があります。EnvDTE代わりにVisual Studioを使用して、 enum. コンパイルする必要なく、IDE で解析された C# ファイルの抽象構文ツリーにアクセスできます。

于 2012-09-24T14:44:33.120 に答える