Cannot reference dependency assemblies in T4 template when using TransformOnBuild

后端 未结 4 988
忘掉有多难
忘掉有多难 2021-01-15 04:20

We\'re trying to use T4 with Visual Studio 2010 (SP1) to build scripts for another language that are based upon some of our existing C# classes. I\'m hoping for the followin

4条回答
  •  情书的邮戳
    2021-01-15 04:50

    My understanding is that Visual Studio 2013 will finally solve this problem, but that doesn't do me much good as I'm still on Visual Studio 2012. After a lot of effort I finally ran across a solution.

    In the project that has the template you wish to run, add the following as a pre-build step on the Build Events tab of the project properties page.

    set textTransformPath="%CommonProgramFiles(x86)%\Microsoft Shared\TextTemplating\$(VisualStudioVersion)\TextTransform.exe"
    if %textTransformPath%=="\Microsoft Shared\TextTemplating\$(VisualStudioVersion)\TextTransform.exe" set textTransformPath="%CommonProgramFiles%\Microsoft Shared\TextTemplating\$(VisualStudioVersion)\TextTransform.exe"
    set ProjectDir=$(ProjectDir)
    %textTransformPath% "%ProjectDir%StringGenerator.tt"
    

    The first two lines take care of the differences between locating TextTransform.exe on 32-bit and 64-bit systems. The third line is the key. I need the path to the project location inside my template, so I set a local environment variable equal to the value of the build's $(ProjectDir) property. Inside my template, just use the following:

    var projectDir = Environment.GetEnvironmentVariable("ProjectDir");
    

    This has solved my issue.

提交回复
热议问题