调用类在同一个项目从T4模板 [英] Calling a class in the same project from T4 template

查看:148
本文介绍了调用类在同一个项目从T4模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创造一些脚手架 CRUD 类,并想利用T4协助建设这些类。我刚刚开始,有一个问题呼吁生活在同一个项目中的类。例如:

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()
            ...
    #>
    }
}

错误:

编译改造:类型或命名空间名称'MyClass的'找不到(?是否缺少using指令或程序集引用)

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

这是一个公共类在同一个项目。我可以从其他类访问它的项目只是没有T4文件。还有一个T4新手。任何方向将AP preciated。

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.

推荐答案

你想做的事不会是直线前进什么。

What you want to do will not be straight forward.

为了为T4使用一个类(即MyClass的)发电机必须引用包含组件或完全在T4中定义的类(这就是EF code发电机做)。此外,您可能正在使用设计时T4(让IDE做代)。设计时间t4发生器将举行一个引用的DLL,所以你将无法再建项目。你实际上必须关闭Visual Studio和重新启动它。 &LT;叹息&GT;

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>

如果您决定进行第一种方法 - 我非常赞同 - 你将要打破你的code为两个组件(3真的)。其中将包含code编写,如MyClass的。第二个用于生成的code。

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.

创建第三个组件(命令行项目),并使用运行T4 在这里。该项目可引用您的第一个组件(与MyClass的),做你想要的任何东西,包括您在此处显示的code!有code产生进入第二组件。

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.

在第一个项目的项目设置,调用第三个项目是一个生成后事件的命令行工具。这也可以在第二个项目的pre生成事件。

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.

我已经创建了几个成功的面向方面编程(AOP)解决方案采用这种方法。唉,这可能不是你所期望的答案。

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.

这篇关于调用类在同一个项目从T4模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆