5

我研究了 google 和 SO,但找不到答案。

我看过以下帖子,但没有成功。

在 T4 模板中使用类

在我的 T4 模板中,我尝试使用自定义类 ResourceManager 中定义的 AddControls 方法,但出现以下错误。

编译转换:找不到类型或命名空间名称“WebApplication1”(您是否缺少 using 指令或程序集引用?)

请帮我。

namespace WebApplication1
{
    public static class ResourceManager
    {

        public static void AddControls(List<string> controlList)
        {
            controlList.Add("Label1");
            controlList.Add("Button1");
        }
     }
}

我的 T4 模板代码如下所示:

<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ output extension=".txt" #>

<#@ assembly name="$(TargetPath)" #>
<#@ import namespace="WebApplication1" #>
<#@ import namespace="System.Collections.Generic" #>


<#
    List<String> controlList = new List<String>();

    ResourceManager.AddControls(controlList);

    foreach (string str in controlList)
    {

        string propName= str;
#>
    My control is  <#=            propName #>

<#
    }

#>
4

1 回答 1

4

构建包含命名空间的项目WebApplication1并再次尝试保存模板。使用您的代码对我有用。

于 2012-07-11T01:10:30.073 回答