我正在尝试跟上 T4 模板的速度。我找到了以下示例(此处):
<#@ template hostspecific="True" #>
<#@ output extension="txt" #>
<#@ include file="T4Toolbox.tt" #>
<#@ import namespace="EnvDTE" #>
<#
CodeEnum enumeration = GetEnum("ContactType.cs");
WriteLine("Found enumeration " + enumeration.Name);
foreach (CodeElement element in enumeration.Children)
{
CodeVariable value = element as CodeVariable;
if (value != null)
WriteLine("… found value " + value.Name);
}
#>
<#+
private CodeEnum GetEnum(string enumFile)
{
ProjectItem projectItem = TransformationContext.FindProjectItem(enumFile);
FileCodeModel codeModel = projectItem.FileCodeModel;
return FindEnum(codeModel.CodeElements);
}
private CodeEnum FindEnum(CodeElements elements)
{
foreach (CodeElement element in elements)
{
CodeEnum enumeration = element as CodeEnum;
if (enumeration != null)
return enumeration;
enumeration = FindEnum(element.Children);
if (enumeration != null)
return enumeration;
}
return null;
}
#>
不知何故,EnvDTE 命名空间中的所有类型都无法识别。我正在使用 Visual T4 扩展。所有 EnvDTE 类型都用红色下划线。模板没有运行,我得到一个错误列表,例如:
The type or namespace ... could not be found (are you missing a using directive or assembly reference?)
有谁知道如何解决这个问题?