得到装饰用T4 / EnvDTE特定属性的所有方法 [英] Get all methods that are decorated with a specific attribute using T4/EnvDTE

查看:610
本文介绍了得到装饰用T4 / EnvDTE特定属性的所有方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得我的项目的所有公用方法,通过使用T4 MyAttribute 装修清单/ EnvDTE。



我知道这可以用反射来实现,但我不希望加载的组装和反映了它在T4模板,相反,我想用现有的代码文件作为来源。



以下是样板代码我,获取到当前项目

 <参考在互联网上找到code><#@模板调试=真hostspecific =真LANGUAGE =C##> 
<#@集名称=EnvDTE#>
<#@集名称=System.Core.dll#>
<#@导入命名空间=System.Linq的#>
<#@导入命名空间=System.Collections.Generic#>
<#@导入命名空间=System.IO#>
<#@输出扩展=#>CS。

<#
的IServiceProvider _ServiceProvider =(的IServiceProvider)主机;
如果(_ServiceProvider == NULL)
抛出新的异常(主机属性返回意外的值(NULL));

EnvDTE.DTE DTE =(EnvDTE.DTE)_ServiceProvider.GetService(typeof运算(EnvDTE.DTE));
如果(DTE == NULL)
抛出新的异常(无法检索EnvDTE.DTE);

阵列activeSolutionProjects =(阵列)dte.ActiveSolutionProjects;
如果(activeSolutionProjects == NULL)
抛出新的异常(DTE.ActiveSolutionProjects返回null);

EnvDTE.Project dteProject =(EnvDTE.Project)activeSolutionProjects.GetValue(0);
如果(dteProject == NULL)
抛出新的异常(DTE.ActiveSolutionProjects [0]返回null);

#>


解决方案

我想确认你计划使用EnvDTE来获得有关项目的类和方法设计时的信息。在我看来,这比冒险,以反映同一个项目的一个过时的组装更加可靠。



既然你已经得到了你的解决方案的当前项目,你现在应该使用 Visual Studio的CodeModel 的迭代类和它们的方法等我知道这可能相当烦人,但我发现了一个免费的可重复使用的模板.ttinclude,为您提供缓和访问CodeModel方法。你可能想href=\"http://t4-editor.tangible-engineering.com/T4-Editor-Visual-T4-Editing.html\">有形的T4编辑器退房

 <#
//得到一个参考项目这个模板T4
VAR项目= VisualStudioHelper.CurrentProject的;

//从代码模型
VAR allClasses = VisualStudioHelper.GetAllCodeElementsOfType(project.CodeModel.CodeElements,EnvDTE.vsCMElement.vsCMElementClass,FALSE)所有类物品;

//遍历所有类
的foreach(EnvDTE.CodeClass codeClass在allClasses)
{
//获取所有的方法此类
变种allFunctions实施= VisualStudioHelper.GetAllCodeElementsOfType(codeClass.Members,EnvDTE.vsCMElement.vsCMElementFunction,FALSE);
的foreach(在allFunctions EnvDTE.CodeFunction功能)
{
//获得所有属性这种方法装饰有
VAR allAttributes = VisualStudioHelper.GetAllCodeElementsOfType(function.Attributes,vsCMElement.vsCMElementAttribute ,FALSE); //检查
如果System.ObsoleteAttribute存在
如果(allAttributes.OfType< EnvDTE.CodeAttribute>()
。任何(ATT = GT; att.FullName ==System.ObsoleteAttribute ))
{
#><#= function.FullName#>
<#
}
}
}
#>


I'd like to get a list of all public methods in my project that are decorated using MyAttribute using T4/EnvDTE.

I know this can be done with reflection, but I don't want to load the assembly and reflect over it in a T4 template, rather, I want to use the existing code files as the source.

The following is boilerplate code I found on the internet that gets a reference to the current project

<#@ 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");

#>

解决方案

I would like to confirm your plan to use EnvDTE to gain design-time information about your project's classes and methods. In my opinion it's more reliable than risking to reflect an outdated assembly of the same project.

Since you already got the current project of your solution you should now use the Visual Studio CodeModel to iterate your classes and their methods etc. I know this can be pretty annoying, but I found a free reusable .ttinclude template that provides you with methods easing the access to the CodeModel. You might want to check out tangible's T4 Editor. It's free and ships with a free template gallery that contains one named "tangible Visual Studio Automation Helper". Using this template your resulting code could look like this:

<#
// 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 #>
<#          
        }
    }
}
#>

这篇关于得到装饰用T4 / EnvDTE特定属性的所有方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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