The targeted framework version and the Visual Studio version are available during the build operation as MSBuild properties as either defined or reserved properties. These properties can be made available for use in a T4 text template for code generation.
The following procedure is based on VS2017 Community Edition.
- Add a Text Template to your project. Project Menu-> Add New Item
->General -> Text Template. Name the file ProjectInfo.tt.
- From the Solution Explorer window, select ProjectInfo.tt and
right-click on its and select "Properties". Change the "Build
Action" from Content to None.
- Replace the contents of ProjectInfo.tt with the following and save
the file. You will receive an error of "An expression block
evaluated as Null", ignore it.
<#@ template debug="false" hostspecific="true" language="VB" #>
<#@ parameter type="System.String" name="VisualStudioVersion" #>
<#@ parameter type="System.String" name="TargetFrameworkVersion" #>
<#@ assembly name="System.Core" #>
<#@ output extension=".vb" #>
Module ProjectInfo
Public ReadOnly VisualStudioVersion As String = "<#= VisualStudioVersion #>"
Public ReadOnly TargetFrameworkVersion As String = "<#= TargetFrameworkVersion #>"
Public ReadOnly BuildDate As New DateTime(<#= DateTime.Now().Ticks #>L)
End Module
- Save the project and select the it in the Solution Explorer window and right-click
on it. Select "Unload Project". Right-click it again and select
"Edit projname.vbproj".
- Scroll down to the end of the file add the following before the
"" tag.
<!-- Define the parameters available T4 Text templates -->
<ItemGroup>
<T4ParameterValues Include="VisualStudioVersion">
<Value>$(VisualStudioVersion)</Value>
<Visible>false</Visible>
</T4ParameterValues>
<T4ParameterValues Include="TargetFrameworkVersion">
<Value>$(TargetFrameworkVersion)</Value>
<Visible>false</Visible>
</T4ParameterValues>
</ItemGroup>
<!-- the following will cause the T4 template to be processed before the build process begins -->
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\TextTemplating\Microsoft.TextTemplating.targets" />
<PropertyGroup>
<TransformOnBuild>true</TransformOnBuild>
<TransformOutOfDateOnly>false</TransformOutOfDateOnly>
</PropertyGroup>
- Save and close the "vbproj" file. Right-click on the project in the Solution Explorer window and select "Reload project".
- Right-click on the project in the Solution Explorer window and select "Rebuild project".
- In the Solution Explorer window, enable "Show all files" and expand the "ProjectInfo.tt" node and open the "ProjectInfo.vb" file. you should see the following code (the assigned values may differ based on you project configuration).
Module ProjectInfo
Public ReadOnly VisualStudioVersion As String = "15.0"
Public ReadOnly TargetFrameworkVersion As String = "v4.72"
Public ReadOnly BuildDate As New DateTime(636968364980609475L)
End Module
Once you have successfully built your project, the values defined in "ProjectInfo.vb" will be accessible by other code. The file will be regenerated on each build.
Reference Articles:
Code Generation and T4 Text
Templates
Pass build context data into the
templates
Edit: As an alternative to editing the projname.vbproj file, you could also place the statements presented under Step 5 in a text file named Directory.Build.targets that would be place in the project folder. The contents need to be enclosed in a <Project>
tag.
<Project>
statements from Step 5
</Project>