无法在T4模板中使用我的自定义类

时间:2012-07-11 00:43:00

标签: c# t4

我研究过google和SO并且找不到答案。

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

Use class inside a T4 template

在我的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 #>

<#
    }

#>

1 个答案:

答案 0 :(得分:4)

构建包含名称空间WebApplication1的项目,然后再次尝试保存模板。 这对我使用你的代码很有用。

相关问题