相关疑难解决方法(0)

使用T4/EnvDTE获取使用特定属性修饰的所有方法

我想获得一个MyAttribute使用T4/EnvDTE 装饰的项目中所有公共方法的列表.

我知道这可以用反射来实现,但我不希望加载的组装和反映了它在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"); …
Run Code Online (Sandbox Code Playgroud)

c# t4 envdte

11
推荐指数
1
解决办法
4977
查看次数

标签 统计

c# ×1

envdte ×1

t4 ×1