How to output namespace in T4 templates?

前端 未结 6 813
無奈伤痛
無奈伤痛 2020-12-25 11:21

I have a T4 template for a class set up with TextTemplatingFileGenerator Custom Tool in Visual Studio:

<#@ template language=\"C#v3.5\" hostspecific=\"Tru         


        
6条回答
  •  -上瘾入骨i
    2020-12-25 11:33

    Here is what you can do with T4 Toolbox:

    <#@ template language="C#v3.5" hostspecific="True" debug="True" #> 
    <#@ include file="T4Toolbox.tt" #>
    <# 
      var namespaceName = TransformationContext.DefaultNamespace; 
    #> 
    

    DefaultNamespace property of TransformationContext class returns a string with namespace based on the root namespace of your project and the location of your .tt file in it (i.e. it treats folders as namespaces). This way you don't have to specify Custom Tool Namespace property for every instance of your .tt file.

    If you prefer to use the Custom Tool Namespace property, you can pass Host.TemplateFile to the GetCustomToolNamespace method posted by @sixlettervariables.

提交回复
热议问题