【问题标题】:Get all methods that are decorated with a specific attribute using T4/EnvDTE使用 T4/EnvDTE 获取所有使用特定属性修饰的方法
【发布时间】:2013-04-24 16:32:27
【问题描述】:

我想获取我的项目中使用 T4/EnvDTE 使用 MyAttribute 装饰的所有公共方法的列表。

我知道这可以通过反射来完成,但我不想加载程序集并在 T4 模板中对其进行反射,而是想使用现有代码文件作为源代码。

以下是我在互联网上找到的参考当前项目的样板代码

<#@ template debug="true" hostspecific="true" language="C#" #>
<#@ assembly name="EnvDTE" #>
<#@ assembly name="System.Core.dll" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.IO" #>
<#@ output extension=".cs" #>

<#
    IServiceProvider _ServiceProvider = (IServiceProvider)Host;
    if (_ServiceProvider == null)
        throw new Exception("Host property returned unexpected value (null)");

    EnvDTE.DTE dte = (EnvDTE.DTE)_ServiceProvider.GetService(typeof(EnvDTE.DTE));
    if (dte == null)
        throw new Exception("Unable to retrieve EnvDTE.DTE");

    Array activeSolutionProjects = (Array)dte.ActiveSolutionProjects;
    if (activeSolutionProjects == null)
        throw new Exception("DTE.ActiveSolutionProjects returned null");

    EnvDTE.Project dteProject = (EnvDTE.Project)activeSolutionProjects.GetValue(0);
    if (dteProject == null)
        throw new Exception("DTE.ActiveSolutionProjects[0] returned null");

#>

【问题讨论】:

    标签: c# t4 envdte


    【解决方案1】:

    我想确认您计划使用 EnvDTE 获取有关您项目的类和方法的设计时信息。在我看来,这比冒险反映同一个项目的过时程序集更可靠。

    由于您已经获得了解决方案的当前项目,您现在应该使用 Visual Studio CodeModel 来迭代您的类及其方法等。我知道这可能很烦人,但我找到了一个免费的可重用 .ttinclude 模板,它为您提供方法简化了对 CodeModel 的访问。您可能想查看tangible's T4 Editor。它是免费的,并附带一个免费的模板库,其中包含一个名为“有形的 Visual Studio 自动化助手”的模板库。使用此模板,您生成的代码可能如下所示:

    <#
    // get a reference to the project of this t4 template
    var project = VisualStudioHelper.CurrentProject;
    
    // get all class items from the code model
    var allClasses = VisualStudioHelper.GetAllCodeElementsOfType(project.CodeModel.CodeElements, EnvDTE.vsCMElement.vsCMElementClass, false);
    
    // iterate all classes
    foreach(EnvDTE.CodeClass codeClass in allClasses)
    {
        // get all methods implemented by this class
        var allFunctions = VisualStudioHelper.GetAllCodeElementsOfType(codeClass.Members, EnvDTE.vsCMElement.vsCMElementFunction, false);
        foreach(EnvDTE.CodeFunction function in allFunctions)
        {
            // get all attributes this method is decorated with
            var allAttributes = VisualStudioHelper.GetAllCodeElementsOfType(function.Attributes, vsCMElement.vsCMElementAttribute, false);
            // check if the System.ObsoleteAttribute is present
            if (allAttributes.OfType<EnvDTE.CodeAttribute>()
                             .Any(att => att.FullName == "System.ObsoleteAttribute"))
            {
            #><#= function.FullName #>
    <#          
            }
        }
    }
    #>
    

    【讨论】:

    • 我在 VS 的模板列表中没有看到“有形的 Visual Studio 自动化助手”。
    • 我现在看到了。您必须打开一个.tt 文件,此时Tangible T4 菜单将与标准菜单一起出现。这是第一个菜单项。让我试一试。
    • 我可以在模板库中看到 Visual Studio 自动化助手,但在任何地方都没有对 VisualStudioHelper 的引用。有一个名为 DteHelper 的类,但它没有您的示例中指示的方法,例如 GetAllCodeElementsOfType。
    • 确保您使用的是“有形的 Visual Studio 自动化助手”模板。还有一个名为“Visual Studio Automation Helper”的模板不如有形的强大。这能解决您的问题吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-11
    相关资源
    最近更新 更多