我正在努力了解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?)
有人知道如何解决这个问题吗?