简体   繁体   English

使用EnvDTE收集项目中的用户类列表

[英]Collect a list of user classes in a project using EnvDTE

I am having a problem creating a way to list all the classes in my project using EnvDTE for templating interfaces using T4 (based on naming conventions), and none of the documentation out there seems to describe how to do it. 我正在创建一种方法来创建一种方法,使用EnvDTE列出项目中的所有类,使用T4模板化接口(基于命名约定),并且没有任何文档似乎描述了如何操作。 I started out with: 我开始时:

<#@ template debug="true" hostspecific="true" language="C#" #>
<#@ output extension=".cs" #>
<#@ Assembly name="EnvDTE" #>
<#@ Assembly name="System.Core" #>
<#@ import namespace="EnvDTE" #>
<#@ Import Namespace="System.Linq" #>
<#@ Import Namespace="System.Collections.Generic" #>
<#
    var env = (DTE)((IServiceProvider)this.Host)
        .GetService(typeof(EnvDTE.DTE));

... and then I started going sideways. ......然后我开始侧身。 I am able to identify my project, but I am not able to collect the classes in the project that I want to filter into a flat list for creating interfaces for. 我能够识别我的项目,但是我无法在项目中收集我想要过滤到用于创建接口的平面列表中的类。

How can I do this? 我怎样才能做到这一点? I just want the classes in my project. 我只想要我的项目中的类。

Since you are using T4 i would suggest you check out the tangible T4 Editor . 由于您使用的是T4,我建议您查看有形的T4编辑器 In their gallery is a free reusable Template "tangible Visual Studio Automation Helper". 在他们的画廊中是一个免费的可重复使用的模板“有形的Visual Studio Automation Helper”。 With this template you can easily find Code Classes etc. (See my answer to this post Design Time Reflection ). 使用此模板,您可以轻松找到代码类等。(请参阅此帖子设计时间反射的答案)。

If you want to do it on your own you should continue like this: 如果你想自己做,你应该继续这样:

    var project = env.ActiveDocument.ProjectItem.ContainingProject;
foreach(EnvDTE.CodeElement element in project.CodeModel.CodeElements)
{
    if (element.Kind == EnvDTE.vsCMElement.vsCMElementClass)
    {
        var myClass = (EnvDTE.CodeClass)element;
        // do stuff with that class here
    }
}

I removed the recursion that would be necessary. 我删除了必要的递归。 A CodeElement can contain other CodeElements. CodeElement可以包含其他CodeElements。 But this way it's easier to read. 但这种方式更容易阅读。

I don't know to much about T4 templates, but can you use Reflection in one to get your class's 我不太了解T4模板,但是你可以用一个Reflection来获得你的课程

Eg: 例如:

<#@ template debug="true" hostspecific="true" language="C#" #>
<#@ output extension=".cs" #>
<#@ Assembly name="EnvDTE" #>
<#@ Assembly name="System.Core" #>
<#@ import namespace="EnvDTE" #>
<#@ Import Namespace="System.Linq" #>
<#@ Import Namespace="System.Collections.Generic" #>
<#@ Import Namespace="System.Reflection" #>
<#

    var list = Assembly.GetExecutingAssembly().GetTypes().Where(t => t.IsClass);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM