Error while using custom TYPE in TextTemplate(t4)
I created MVC project and added "Class1.cs" to it(in the same project) Class1 code:
u开发者_运维知识库sing 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="$(SolutionDir)\MvcApplication2\bin\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
I found the problem.
Regarding 1st error: when tt file couldn't be generated, this is considered as error and the application is NOT compiled, respectively 'MvcApplication2.dll' doesn't exist.
Solution: comment everything it tt file and build application. Then, uncomment all in tt file and run CustomTool again.
Regarding 2nd error: as I said before, if tt fie couldn't be generated, this is considered as error and the application is NOT compiled, respectively all changes you made are NOT saved, and therefore it says that some type/method couldn't be found(they just doesn't exist in dll)
Solution: to do as in the solution abode.
---BUT---
Even if no error occurred while running CustomTool there will be error while building because VisualStudio and T4 want to use dll when one of them is using it already.
Solution: use T4Toolbox and change
<#@ assembly name="$(SolutionDir)\MvcApplication2\bin\MvcApplication2.dll" #>
to
<#@ VolatileAssembly processor="T4Toolbox.VolatileAssemblyProcessor" name="$(SolutionDir)\MvcApplication2\bin\MvcApplication2.Dll" #>
You can read the article and download the toolbox in GEORGE MATHEW's site here
精彩评论