I'm attempting to create some scaffolding for CRUD
classes and would like use T4 to assist in building these classes. I'm just getting started and having an issue calling a class that lives in the same project. Ex:
<#@ import namespace="System.Collections.Generic" #>
<#@ template language="C#" #>
<#@ output extension=".cs" #>
<#@ include file="T4Toolbox.tt" #>
using System;
using System.Data;
using System.Data.Linq;
using System.Collections.Generic;
namespace TTFileGenerator
{
<#var entityName = "TEST";#>
public class <#=entityName#>
{
<#
MyClass myClass = new MyClass();
List<string> something = myClass.GetSomething()
...
#>
}
}
The error:
Compiling transformation: The type or namespace name 'MyClass' could not be found (are you missing a using directive or an assembly reference?)
It is a public class in the same project. I can access it from other classes in the project just not the T4 file. Still a T4 newbie. Any direction would be appreciated.
Design-time T4 text templates let you generate program code and other files in your Visual Studio project. Typically, you write the templates so that they vary the code that they generate according to data from a model. A model is a file or database that contains key information about your application's requirements.
What you want to do will not be straight forward.
In order for T4 to use a class (i.e. MyClass) the generator has to reference the containing assembly or a class defined entirely in T4 (this is what the EF code generators do). Furthermore, you are probably using design-time T4 (letting the IDE do the generation). The design time t4 generator will hold a reference to the dll, so you will not be able to build the project again. You will actually have to shut down Visual Studio and restart it. <sigh>
If you decide to proceed with the first approach -- which I highly endorse -- you will want to break your code into two assemblies (3 really). One which will contain code you write, like MyClass. The second for the code that is generated.
Create a third assembly (command line project) and use runtime t4 in here. This project can reference your first assembly (with MyClass) and do anything you want with it, including the code that you have shown here! Have the code generate into the second assembly.
In the project settings of the first project, call the command line tool of the third project as a post-build event. This can also be the pre-build event on the second project.
I have created several successful Aspect Oriented Programming (AOP) solutions with this method. Alas, it may not be the answer you were hoping for.
If you are really brave, you can have the command line tool generate back into the original project. This would require you to compile twice. I cannot recommend this at this time.
Figured I would add some code to help others, although Phillip hit the nail on the head.
I had to break out the class I wanted to call into another assembly and make an assembly reference with a relative path. I didn't see any locking (VS 2012)... You just need to remember to build this assembly if you make any changes prior to transforming your T4 templates:
//Reference to another project (for testing, it lives in the debug folder)
<#@ assembly name="$(SolutionDir)\MyAssembly\bin\x86\Debug\MyAssembly.dll"#>
<#
var helper = new Utilities.MyHelperClass();
#>
<#
var something = helper.GetSomething(param1, param2);
#>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With