Calling a class in the same project from T4 template

ぐ巨炮叔叔 提交于 2019-11-29 04:28:06

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);
#>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!