【问题标题】:EnvDTE types are not recognized in T4 templateT4 模板中无法识别 EnvDTE 类型
【发布时间】:2012-09-08 21:49:36
【问题描述】:

我正在努力跟上 T4 模板的速度。我找到了以下示例(here):

<#@ template hostspecific="True" #>
<#@ output extension="txt" #>
<#@ include file="T4Toolbox.tt" #>
<#@ import namespace="EnvDTE" #>
<#
  CodeEnum enumeration = GetEnum("ContactType.cs");
  WriteLine("Found enumeration " + enumeration.Name);
  foreach (CodeElement element in enumeration.Children)
  {
    CodeVariable value = element as CodeVariable;
    if (value != null)
      WriteLine("… found value " + value.Name);
  }
#>
<#+
  private CodeEnum GetEnum(string enumFile)
  {
    ProjectItem projectItem = TransformationContext.FindProjectItem(enumFile);
    FileCodeModel codeModel = projectItem.FileCodeModel;
    return FindEnum(codeModel.CodeElements);
  }

  private CodeEnum FindEnum(CodeElements elements)
  {
    foreach (CodeElement element in elements)
    {
      CodeEnum enumeration = element as CodeEnum;
      if (enumeration != null)
        return enumeration;
      enumeration = FindEnum(element.Children);
      if (enumeration != null)
        return enumeration;
    }
    return null;
  }
#>

不知何故,EnvDTE 命名空间中的所有类型都无法识别。我正在使用 Visual T4 扩展。所有 EnvDTE 类型都用红色下划线。模板没有运行,我得到一个错误列表,例如:

The type or namespace ... could not be found (are you missing a using directive or assembly reference?)

有人知道怎么解决吗?

【问题讨论】:

    标签: c# t4 envdte


    【解决方案1】:

    您是否在项目中添加了对 ENVDTE 和 ENVDTE80(90 等)的引用?

    【讨论】:

    • 谢谢!这确实是问题所在。想想就很合乎逻辑......非常感谢!
    【解决方案2】:

    尝试这样使用

     DTE env = GetVSEnvironment();    
    

    ....

    private DTE GetVSEnvironment() {
                DTE env = null;
                var provider = Host as IServiceProvider;
                if (provider != null) {
                    env = provider.GetService(typeof(DTE)) as DTE;
                }
    
                if (env == null) {
                    throw new InvalidOperationException("Template must be executed from Visual Studio");
                }
    
                return env;
            }
    

    现在你做env.blablabla 例如:env.Solution.FindProjectItem(Host.TemplateFile).ContainingProject;

    【讨论】:

    • @vibeeshanRC 我可以吻你!我遇到了 VS2012 的问题,这对我有用。
    【解决方案3】:

    嗯,我认为以下包括

    <#@ template hostspecific="True" #>
    

    会拉入程序集,但也许不会。首先,尝试将以下内容添加到模板顶部。

    <#@ Assembly Name="EnvDTE" #>
    

    如果这不起作用,请尝试添加完整路径。对我来说,它的

    <#@ Assembly Name="C:\Program Files (x86)\Common Files\microsoft shared\MSEnv\PublicAssemblies\envdte.dll" #>
    

    【讨论】:

    • 我都试过了,第一个什么也没做。第二个产生以下错误:Error 1 Compiling transformation: An assembly with the same identity 'EnvDTE, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' has already been imported. Try removing one of the duplicate references. D:\jkokorian\My Documents\Visual Studio 2010\Projects\T4TemplatesTestMVC4\T4TemplatesTestMVC4\Models\MyFirstTemplate.tt
    • @jkokorian:好吧,至少您知道程序集已加载。卸载/重新安装 T4 编辑器?也许换一个?
    • 还可以尝试将您的 CustomTool 属性设置为“TextTemplatingFileGenerator”而不是默认的“TextTemplatingFillePreProcessor”。在解决方案资源管理器中选择您的 .tt 文件,右键单击并查看属性:)
    【解决方案4】:

    添加这行对我有用:

    <#@ Assembly Name="EnvDTE" #>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-21
      • 2012-09-27
      • 1970-01-01
      • 2020-05-14
      相关资源
      最近更新 更多