locked
T4 template basic question RRS feed

  • Question

  • Hello,

    I have an IService interface and I would like to generate an IServiceAsync interface using a t4 template.
    If IService is in other assembly I would compile that assembly first and use reflection to generate IServiceAsync.
    But I need both interfaces to be in the same assembly.
    How can I do it?

    Thanks.


    Enterprise Library
    Saturday, August 14, 2010 5:46 PM

Answers

  • Heraclito,

    I think I understand what you want.

    In your code, you frequently write interfaces in pairs. Each interface has an async version:

    interface IService1
    {
      T1 A();
      T2 B();
    }
    interface IService1Async
    {
      T1 Aasync();
      T2 Basync();
    }
    

    When you write IService2, IService3,... you want to just write the basic versions by hand, and then generate the async versions automatically. You'd have all the async versions in a separate file.

    This is possible - your text template reads the file containing the basic declarations. You need to use System.CodeDom.Compiler to parse the interface declarations. As you say, you can't reflect over them before they are compiled.

    Also might be useful: If you set hostspecific="true" in the template directive, can cast this.Host to IServiceProvider to access Visual Studio features. You can also use Host.ResolvePath(filename) to obtain the absolute path of a file in the project.


    - Alan -MSFT
    Tuesday, August 17, 2010 6:11 PM

All replies

  • Hello,

    Could elaborate more about your need?

    For T4, T4 use text template(tt file) to generate VB.net or C# code, it does not use existing interface to generate code.

    So I could not quite understand you.

    Thanks

    Chao


    Please unmark it if it does not help, and mark it if it helps.
    Tuesday, August 17, 2010 7:46 AM
  • Heraclito,

    I think I understand what you want.

    In your code, you frequently write interfaces in pairs. Each interface has an async version:

    interface IService1
    {
      T1 A();
      T2 B();
    }
    interface IService1Async
    {
      T1 Aasync();
      T2 Basync();
    }
    

    When you write IService2, IService3,... you want to just write the basic versions by hand, and then generate the async versions automatically. You'd have all the async versions in a separate file.

    This is possible - your text template reads the file containing the basic declarations. You need to use System.CodeDom.Compiler to parse the interface declarations. As you say, you can't reflect over them before they are compiled.

    Also might be useful: If you set hostspecific="true" in the template directive, can cast this.Host to IServiceProvider to access Visual Studio features. You can also use Host.ResolvePath(filename) to obtain the absolute path of a file in the project.


    - Alan -MSFT
    Tuesday, August 17, 2010 6:11 PM
  • Thanks Alan, yes, that is, sorry for the confusing presentation.
    I will explore System.CodeDom.Compiler and "this.Host", and see how it's going.


    Enterprise Library
    Wednesday, August 18, 2010 1:47 PM