通过DTE在C#中T4模板访问项目 [英] Accessing Projects via DTE in C# T4 Template

查看:374
本文介绍了通过DTE在C#中T4模板访问项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在试图遍历所有我的项目(​​的SharePoint),以获得所有功能的GUID到一个文件中。在那里,我想$ P $的项目名称PFIX他们。我的问题是DTE.Solution.Item和DTE.Solution.Projects.Item(或的foreach普查员)将不会采取一个整数作为参数和foreach返回一个对象,它是不强制转换为项目。

I'm currently trying to iterate over all of my projects (sharepoint) to get all feature guids into an file. there i want to prefix them with the projects name. My problem is DTE.Solution.Item and DTE.Solution.Projects.Item (or the enumerators for foreach) will not take an integer as parameter and foreach returns an object which is not castable to Project.

下面是我的code片断:

Here is my code snippet:

var hostServiceProvider = (IServiceProvider) Host;
var dte = (DTE) hostServiceProvider.GetService(typeof(DTE));
var projectCount = dte.Solution.Projects.Count;

var projects = new Dictionary<string, string>();

foreach(Project dteProject in dte.Solution)
{
    var dteProject = dte.Solution.Item(i);
    projects.Add(dteProject.Name, dteProject.FullName);
}

好吧 - 在code是好的 - 调试器不!
我的例外情况在调试情况下抛出,但模板会运行良好,如果调试器没有连接。

Okay - the code is alright - the debugger is NOT! My Exceptions where thrown in a debug context, but the Template will run fine, if the debugger is not attached.

推荐答案

尝试解决方案.Projects 属性:

<#@ template language="C#" debug="true" hostspecific="true" #>
<#@ assembly name="Microsoft.VisualStudio.Shell.Interop.8.0" #>
<#@ assembly name="EnvDTE" #>
<#@ assembly name="EnvDTE80" #>
<#@ assembly name="VSLangProj" #>
<#@ import namespace="Microsoft.VisualStudio.Shell.Interop" #>
<#@ import namespace="EnvDTE" #>
<#@ import namespace="EnvDTE80" #>
<#@ import namespace="Microsoft.VisualStudio.TextTemplating" #>
<#@ output extension=".txt" #>
<#

var hostServiceProvider = (IServiceProvider)this.Host;
var dte = (DTE)hostServiceProvider.GetService(typeof(DTE));

foreach (Project project in dte.Solution)
{
    #>
    <#= project.Name #>
    <#
}

#>

这篇关于通过DTE在C#中T4模板访问项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆