How to enable custom utility class with our T4 template generators?

How to enable custom utility class with our T4 template generators? I tried to add a module to the project, but there seems to be no way to enable it for my generators to use methods from it.

Thanks.

+4
source share
2 answers

You cannot access the code that is in your project from your template. Even if you could, how would this happen? The template is executed at design time.

Use the Inherits directive, specify the name of your class. http://technet.microsoft.com/en-us/query/bb126474 See "Using a different set of utility methods." Your class should inherit from Microsoft.VisualStudio.TextTemplating.TextTransformation

<#@ template inherits="MyUtilityClass" #> 

This, or you can create your module in the assembly, and then refer to the assembly from the template

 <#@ assembly name="MyAssembly.Utilities" #> 

or you can just put your utility methods in a class function block in another template and then include it in your main template

 <#@ include file="MyUtilities.tt" #> 
+5
source

If you do not use templates in ASP.Net MVC, you can simply put your utility in a file and use the directive <# @include file = "foo.t4" #> to insert it. inside the method in <# + block #> and just call it.

If you use MVC, then the base class is the path to @ILovePaperTowels.

0
source

Source: https://habr.com/ru/post/1384378/


All Articles