Click here to Skip to main content
15,888,283 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I created MVC project and added "Class1.cs" to it(in the same project)
Class1 code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MvcApplication2.Models;
namespace MvcApplication2
{
    public class EntityAttribute
    {
        public string Name { get; set; }
    }
    public class Entity
    {
        public List<EntityAttribute> Attributes { get; set; }
        public string Name { get; set; }
    }
    public class Class1
    {
        public static string getTable()
        {
            return "tbl";
        }
    }
}


I have Text Template file .tt:
<#@ template debug="true" language="C#" #>
<#@ output extension=".cs" #>
<#@ assembly name="MvcApplication2.dll" #>
<#@ import namespace="MvcApplication2" #>
<#
    string s = Class1.getTable();
#>

When I run CustomTool on TextTemplate I get the following error:
Error 2 Compiling transformation: Metadata file 'MvcApplication2.dll' could not be found C:\Users\Igor\Documents\Visual Studio 2010\Projects\MvcApplication2\MvcApplication2\TextTemplate1.tt 1 1 MvcApplication2
When I change "<#@ assembly name="MvcApplication2.dll" #>" to full path "<#@ assembly name="C:\Users\Igor\Documents\Visual Studio 2010\Projects\MvcApplication2\MvcApplication2\bin\MvcApplication2.dll" #>"

I get the following error:
Error 2 Compiling transformation: 'MvcApplication2.Class1' does not contain a definition for 'getTable' c:\Users\Igor\Documents\Visual Studio 2010\Projects\MvcApplication2\MvcApplication2\TextTemplate1.tt 8 20

Where is my mistake??
Thank you
Posted

1 solution

You need to compile the assembly with the new method before the template can be executed because it links the compiled assambly. So it will hardly work with the same project.

If it's not in the same project, then make sure you are referencing the last build of it, may be you are accessing an out-dated local copy or an out-dated GAC copy. Of course this is not the case if you use the full path. [By the way, use $(SolutionDir), $(ProjectDir) and $(OutDir) they are very handy here, for example: <#@ assembly name="$(SolutionDir)YourProjectName\$(OutDir)\YourAssemblyName.dll" #>].

If you need to use custom clases in your T4, I recommend you to put them in a separate project, one that has no dependency on the one where you use the template. This way compiling the project will not be a problem before using it in the template.

You may be interested in some other techniques too... for example using T4 to generate a T4 file. Or using T4 to read from a file** and output it's content, perhaps modified. And of course, you can use T4 to write to files, if those files are in the project, you have a T4 with multiple outputs.

**The file may be in a another project, perhaps a dummy project with dummy build actions. Such as this: http://www.darinhiggins.com/creating-a-dummy-build-project-in-vs2008/[^]

How to update GAC on build:
Automatically GAC an assembly after a build and include debug info.[^]

Check out OlegSych blog:
http://www.olegsych.com/articles/[^]

http://www.olegsych.com/2008/02/t4-assembly-directive/[^]

Opps, I'm giving away some of my "secrets" :P

Hope this helps you out.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900