T4 template adding assembly of existing project in solution

和自甴很熟 提交于 2019-12-02 19:58:58

T4 works nearly completely independent from the rest of your code. You're on the right track though, with the assembly directive, but you'll need to specify a full path to the actual DLL of the assembly, unless the assembly is in the GAC (which it probably isn't).

Luckily, however, you can use MSBuild macros in T4 directives. So, you'll probably get something like

<#@ assembly name="$(SolutionDir)Project.Common.WebApi\bin\Debug\Project.Common.WebApi.dll" #>

See MSDN for some more background on this syntax.

You still also need the import namespace directive.

Finally, be wary of project build order. The project that contains your T4 template now depends on Project.Common.WebApi, so you'll need to make sure that Project.Common.WebApi is built first. Otherwise, your T4 template might accidentally link to an older version of the assembly, making bugs really hard to track.

If you already have a project reference to it, you're all done, but otherwise you need to set up the dependencies correctly. You can do this in Visual Studio via the "Project Dependencies..." dialog. Right-click the project to find it.

In case your project with T4 template has direct reference to the project, you can use $(TargetDir)

<#@ assembly name="$(TargetDir)Project.Common.WebApi.dll" #>

<#@ assembly name="$(TargetPath)" #>

As simple as that.

Just in case you are using C# use a double slash \\.

I think it would be like:

<#@ assembly name="$(SolutionDir)Project.Common.WebApi\\bin\\Debug\\Project.Common.WebApi.dll" #>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!