Reputation: 2571
I have build an Addin for code generation (C#, VS 08) for our team.
The addin creates a new menu entry if i click on a file in the solution explorer. There i can choose a destination test project where the 2 files are generated to.
For the code generation process i need informations from the selected item in the solution explorer (like Interfaces, generic types of the class declaration).
My source class looks like
public class CustomerLogic : BaseBL<T1, T2, T3>, ICustomerBL
The generated container class
public class CustomerContainer : BaseBLDummyContainer<T1, T2, T3>
The generated dummy class looks like
public class CustomerBLDummy : BaseBLDummy<T1, T2, T3, CustomerContainer>, ICustomer
How i realized it?
I created two templates (container and dummy), put placeholder to the spaces so the two template files look like
Containertemplate
public class $Classname$ : BaseBLDummyContainer<$T1$, $T2$, $T3$>
Dummytemplate
public class $Classname$ : BaseBLDummy<$T1$, $T2$, $T3$, $TContainer$>$, TInterface$
To generate the templates i have writen some code to
You can see, a "lot" of work to do only to generate some code.
Thanks a lot.
Upvotes: 0
Views: 239
Reputation: 6606
You don't need to make an either/or choice here 'k', you can mix and match add-ins and T4.
T4 has a very simple UI binding to Visual Studio out of the box, that just uses the Custom Tool mechanism (IVsSingleFileGenerator) to connect a template file to an output file in the project. (See T4Toolbox community project for more complex output).
However, T4 also exposes a service interface in VS (STextTemplating/ITextTemplating) that you can use from both VS add-ins and VS packages.
So you could keep your add-in's entry point and core way of working, but use T4 to provide the template engine and avoid having to maintain that part yourself. You'd likely use the <#@ parameter #> directive to pass replacement parameters in to your template for T1, T2, T3 etc.
Hope this gives you some ideas for options to move forward.
Upvotes: 1