Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to output namespace in T4 templates?

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

<#@ template language="C#v3.5" hostspecific="True" debug="True" #>
<#
  var className = System.IO.Path.GetFileNameWithoutExtension(Host.TemplateFile);
  var namespaceName = "MyNamespace";
#>

namespace <#= namespaceName #>
{
    public static class <#= className #>
    {
        // some generated code
    }
}

How can I get the value of the "Custom Tool Namespace" property in Visual Studio, so I don't have to hardcode the namespace?

I would even be happy with the default namespace for the C# project.

like image 967
Hallgrim Avatar asked Jan 11 '10 14:01

Hallgrim


People also ask

What is true about T4 templates in Entity Framework?

T4 templates in entity framework are used to generate C# or VB entity classes from EDMX files. Visual Studio 2013 or 2012 provides two templates- EntityObject Generator and DBContext Generator for creating C# or VB entity classes. The additional templates are also available for download.

What is .TT file in Entity Framework?

tt" extension indicates a T4 template file. In this case the template file is responsible for generating the classes that are represented by the Entity Model defined in your edmx file.

How do TT files work?

Template file created by Visual Studio, a software development tool created by Microsoft; contains both text blocks and control logic used for generating new text files; can be written using Visual C# or Visual Basic code; used for both runtime text generation as well as source code generation.


4 Answers

If you're using Visual Studio 2010, you can retrieve the namespace by checking the CallContext's "NamespaceHint" property.

System.Runtime.Remoting.Messaging.CallContext.LogicalGetData("NamespaceHint"); 
like image 66
GarethJ Avatar answered Sep 16 '22 18:09

GarethJ


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.

like image 39
Oleg Sych Avatar answered Sep 17 '22 18:09

Oleg Sych


Damien Guard includes some code in a blog posting which retrieves the Custom Tool Namespace for a given file:

public override String GetCustomToolNamespace(string fileName)
{
    return dte.Solution.FindProjectItem(fileName).Properties.Item("CustomToolNamespace").Value.ToString();
}
like image 36
user7116 Avatar answered Sep 18 '22 18:09

user7116


How I've done this:

<#@ assembly name="EnvDTE" #>
<#@ import namespace="EnvDTE" #>

<# 
    // Get value of 'Custom Tool Namespace'
    var serviceProvider = (IServiceProvider)this.Host;
    var dte = (DTE)serviceProvider.GetService(typeof(DTE));    
    var Namespace = dte.Solution.FindProjectItem(this.Host.TemplateFile).Properties.Item("CustomToolNamespace").Value;
 #>

namespace <#= Namespace #> {

}
like image 36
Damian Drygiel Avatar answered Sep 18 '22 18:09

Damian Drygiel