tags:

views:

80

answers:

1

I just finished creating T4 templates to auto-generate property implementations for my ViewModel classes (see here: http://stackoverflow.com/questions/2968406/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?

+1  A: 

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.

mattmc3
@matt: can you provide some links?
John Saunders
Sure... unfortunately, a lot of people were writing about this when VS2010 was in beta, and not much press has been given to it since. But it's not too hard to understand once you've done one yourself. Here are some of the del.icio.us links I had from when I was learning:http://msdn.microsoft.com/en-us/library/ee844259.aspxhttp://karlshifflett.wordpress.com/2009/10/30/t4-preprocessed-text-templates-in-visual-studio-2010/http://www.olegsych.com/2009/09/t4-preprocessed-text-templates/
mattmc3