chrischu
chrischu

Reputation: 3127

Assembly with T4 templates for use in other Solutions?

I just finished creating T4 templates to auto-generate property implementations for my ViewModel classes (see here: Automatic INotifyPropertyChanged Implementation through T4 code generation?).

Currently I need to have the project holding the ".tt"-files in the solution where I want to generate the property implementations.

So the solution for example contains three projects: T4Generation, SomeProjectWithViewModels, AnotherProjectWithViewModels.

Then when invoking the T4-template in T4Generation it looks through all of the projects in the solution and finds all of the ViewModel classes and generates a C# file containing property implementations for the specific ViewModel in the respective project.

Example:

"SomeProjectWithViewModels.SomeViewModel.cs"

public partial class SomeViewModel : BaseViewModel
{
    private string p_SomeProperty;
}

generates a file "SomeProjectWithViewModels.SomeViewModel.Properties.cs"

public partial class SomeViewModel
{
    public string SomeProperty
    {
        get { ... }
        set { ... }
    }
}

The question I now have if there is any possibility to create a assembly (ie "T4Generation.dll") that can be referenced in any solution enabling the host solution to invoke the code generation process somehow.

For example I would start a new solution "SomeSolution.sln" with projects "SomeApplication.exe", "SomeClassLibrary.dll" and in SomeApplication I would reference "T4Generation.dll" and during the build process of SomeApplication the T4 generation would be invoked.

Is this possible?

Upvotes: 1

Views: 895

Answers (1)

mattmc3
mattmc3

Reputation: 18345

Are you using Visual Studio 2010? If so, I'd recommend using the "Preprocessed Templates" for what you're trying to do. Preprocessed templates make a generator class that generates your code, instead of generating your code directly. So, with these generator classes made from your T4 templates, you can make a T4Generation.EXE console app that gets called from your projects as a pre-compile command. Pass in the path to the project, and the generators should do the rest. This will then be re-usable from any project.

Upvotes: 1

Related Questions