Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can not use my custom class in T4 template

Tags:

c#

t4

I have researched google and SO and can not find answer.

I have looked at following post with out any success.

Use class inside a T4 template

In my T4 template, I am trying to use AddControls Method defined in my Custom class ResourceManager but I am getting the following error.

Compiling transformation: The type or namespace name 'WebApplication1' could not be found (are you missing a using directive or an assembly reference?)

Please help me.

namespace WebApplication1
{
    public static class ResourceManager
    {

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

My T4 Template code looks as follows:

<#@ 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 #>

<#
    }

#>
like image 377
dotnet-practitioner Avatar asked Oct 08 '22 06:10

dotnet-practitioner


1 Answers

Build the project that contains namespace WebApplication1 and try saving the template again. That worked for me using your code.

like image 156
dan radu Avatar answered Oct 10 '22 09:10

dan radu