简体   繁体   English

如何使用EnvDTE获取类的using语句?

[英]How to get the using statements for a class using EnvDTE?

I am working on a T4 template which produces partial classes based on existing partial classes. 我正在研究一个T4模板,该模板根据现有的部分类生成部分类。

Sometimes the generated code will reference types being used from the existing (non-generated) codebase. 有时,生成的代码将引用现有(未生成)代码库中正在使用的类型。

The generated code must either fully qualify these types, or mimic the using statements it finds in the non-generated code. 生成的代码必须完全限定这些类型,或者模仿它在非生成的代码中找到的using语句。

Mimicking using statements seems better since it will support cases where the type is being referenced from a [Attribute(typeof(Something))] , where EnvDTE only returns the string literal "typeof(Something)" . 模仿using语句似乎更好,因为它将支持从[Attribute(typeof(Something))]引用类型的情况,其中EnvDTE仅返回字符串文字"typeof(Something)"

So: how do I find these using statements? 那么:如何找到这些using语句? I'm using tangible T4's AutomationHelper, but still can't seem to find a solution :( 我正在使用有形的T4的AutomationHelper,但似乎仍然找不到解决方案:(

You can get the using statements by looking at the FileCodeModel.CodeElements for the ProjectItem. 您可以通过查看ProjectItem的FileCodeModel.CodeElements来获得using语句。

Each ProjectItem has a FileCodeModel property. 每个ProjectItem都有一个FileCodeModel属性。 The FileCodeModel.CodeElements will contain a CodeImport for each using statement. FileCodeModel.CodeElements将为每个using语句包含一个CodeImport。 Note that the FileCodeModel.CodeElements will contain other things not just CodeImportss o you will need to check the type returned or filter the unwanted types. 请注意,FileCodeModel.CodeElements将包含其他内容,而不仅仅是CodeImportss,您将需要检查返回的类型或过滤不需要的类型。

An example is shown below. 一个例子如下所示。 Here I am using the NuGet's Package Manager Console and PowerShell. 在这里,我正在使用NuGet的Package Manager控制台和PowerShell。

$p = Get-Project
$fileCodeModel = $p.ProjectItems.Item("Class1.cs").FileCodeModel
$fileCodeModel.CodeElements | % { $_.Namespace }

The code above assumes there is a Class1.cs file in the root of the project. 上面的代码假定项目的根目录中有一个Class1.cs文件。 For each using statement it will print the full namespace. 对于每个using语句,它将打印完整的名称空间。 Note that in the above code it is trying to print the Namespace for each CodeElement and some of the elements will not have this property so you will need to restrict this so it only looks at CodeImport types. 请注意,在上面的代码中,它正在尝试为每个CodeElement打印名称空间,并且某些元素将不具有此属性,因此您将需要对此进行限制,以便仅查看CodeImport类型。 The above will work for the following class file: 上面的内容适用于以下类文件:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ClassLibrary1
{
    public class Class1
    {
    }
}

If you have using statements between the namespace ClassLibrary1 and the public class Class1 part you will need to do more work and look at the CodeNamespace members since the CodeImports will not be available directly from the FileCodeModel.CodeElements, but hopefully the above code should point you in the right direction. 如果您在命名空间ClassLibrary1公共类Class1部分之间使用了语句,则由于CodeImports不能直接从FileCodeModel.CodeElements获得,因此您需要做更多的工作并查看CodeNamespace成员,但希望上面的代码能为您提供帮助在正确的方向。

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

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