visual-studio 如何在 T4 模板中输出命名空间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2042344/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 11:23:04  来源:igfitidea点击:

How to output namespace in T4 templates?

c#visual-studiocode-generationt4

提问by Hallgrim

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

我有一个 T4 模板,用于在 Visual Studio 中使用 TextTemplatingFileGenerator 自定义工具设置的类:

<#@ 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?

如何在 Visual Studio 中获取“自定义工具命名空间”属性的值,以便不必对命名空间进行硬编码?

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

我什至会对 C# 项目的默认命名空间感到满意。

回答by GarethJ

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

如果您使用的是 Visual Studio 2010,则可以通过检查 CallContext 的“NamespaceHint”属性来检索命名空间。

System.Runtime.Remoting.Messaging.CallContext.LogicalGetData("NamespaceHint");

回答by Oleg Sych

Here is what you can do with T4 Toolbox:

您可以使用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.

TransformationContext 类的 DefaultNamespace 属性返回一个带有命名空间的字符串,该字符串基于项目的根命名空间和 .tt 文件在其中的位置(即将文件夹视为命名空间)。这样您就不必为 .tt 文件的每个实例指定自定义工具命名空间属性。

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

如果您更喜欢使用自定义工具命名空间属性,可以将 Host.TemplateFile 传递给@sixlettervariables 发布的 GetCustomToolNamespace 方法。

回答by user7116

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

Damien Guard 在博客文章中包含一些代码,用于检索给定文件的自定义工具命名空间

public override String GetCustomToolNamespace(string fileName)
{
    return dte.Solution.FindProjectItem(fileName).Properties.Item("CustomToolNamespace").Value.ToString();
}

回答by Damian Drygiel

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 #> {

}

回答by alduar

If you use Visual Studio 2012

如果您使用 Visual Studio 2012

EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile);
string namespaceName = System.Runtime.Remoting.Messaging.CallContext.LogicalGetData("NamespaceHint").ToString();

EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager.Create(this);

Aldo Flores @alduar

奥尔多弗洛雷斯@alduar

回答by Nick Prince

The accepted answer doesn't work on Visual Basic Projects. I had to use the method from: http://lennybacon.com/post/2010/12/10/generatingcodefileswithcorrectnamespacesusingt4

接受的答案不适用于 Visual Basic 项目。我不得不使用以下方法:http: //lennybacon.com/post/2010/12/10/generatecodefileswithcorrectnamespacesusingt4

var hostServiceProvider = (IServiceProvider)Host;
var dte = (EnvDTE.DTE)hostServiceProvider.GetService(typeof(EnvDTE.DTE));
var activeSolutionProjects = (Array)dte.ActiveSolutionProjects;
var dteProject = (EnvDTE.Project)activeSolutionProjects.GetValue(0);
var defaultNamespace = dteProject.Properties.Item("DefaultNamespace").Value;