Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
460 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...