T4 Template Reuse RRS feed

  • Question

  • I'm just getting into T4, and the samples I look at all have the .tt file in the same assembly that is consuming the generated code.

    I was wondering how (or even if) you can build an library that contains T4 templates, that can then be consumed (referenced) by another assembly which in turn causes code to be generated based on it's environment/variables.

    Is this the kind of thing you can/should do with T4.  If so, what kind of articles, keywords do I search for.  Many thanks.

    Phil Cockfield
    Tuesday, May 11, 2010 10:19 PM


All replies

  • Hi Phil,

    I am not quite understanding your question. Do you mean that you want to reference an assembly containing T4 templates and the T4 templates will be generated differently dependingon the referenced assembly?

    Generally, you can directly reference assembly with T4 templates and use the generated code. But I am not sure in what cases that the referenced assembly will affect the generated code. Can you give me an example?


    Hongye Sun [MSFT]
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg @ microsoft.com

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    See what's happening in MSDN forum? Follow us at Twitter.
    Wednesday, May 12, 2010 7:53 AM
  • Thanks Hongye for getting back to me.


    Yes, here's the example I'm trying to use T4 for.  Say I have:

    - AssemblyA, and

    - AssemblyB


    AssemblyA contains a T4 template that constructs a set of classes based on various Type's in an assembly that are marked with the attribute [GenerateHelper].

    Suppose in AssemblyB I have a a type:

      class Foo



    I was thinking I'd like to be able to reference AssemblyA and somehow have the helper class generated for 'Foo'.

    See he difference here, I'm trying to use AssemblyA as a code-gen factory.  But maybe this is not how T4 is designed (??).



    Phil Cockfield
    Wednesday, May 12, 2010 12:15 PM
  • Phil,

    Perhaps this would help: http://www.olegsych.com/2008/10/t4-tutorial-reusing-code-generators-on-multiple-projects/

    In 2010 the reuse gets easier with support for relative paths in the include directive and preprocessed templates: http://www.olegsych.com/2009/09/t4-preprocessed-text-templates/.

    Hope this helps,

    Friday, May 14, 2010 11:52 AM
  • Oleg!  You are the man!!!


    I think both of those posts are going to get me moving in the right direction.  Appreciate the pointer.

    Phil Cockfield
    Saturday, May 15, 2010 5:07 PM