VS2022中.NET Framework4.8项目如何配置TT文件仅在Release模式生成?
仅在Release模式启用.NET Framework 4.8项目的TT文件代码生成
我正在使用VS2022开发目标框架为.NET Framework 4.8的C#项目,需要为Debug和Release模式配置差异化设置:仅在Release模式下将.tt文件加入项目并启用代码生成,Debug模式下不执行此操作*。
具体来说,我希望以下配置仅在Release编译时生效,Debug模式下不生效:
<ItemGroup> <Content Include="..\AssemblyCopyright.tt"> <Link>Properties\AssemblyCopyright.tt</Link> <Generator>TextTemplatingFileGenerator</Generator> <LastGenOutput>AssemblyCopyright.cs</LastGenOutput> </Content> </ItemGroup>
动机
需要根据当前年份动态生成版权声明,因此采用代码模板(TT)生成方式实现,但该操作会减慢编译速度。因此仅希望在为用户构建Release版本时生成版权声明,调试测试时不生成。
问题重现
我尝试给ItemGroup添加条件<ItemGroup Condition=" '$(Configuration)' == 'Release' ">,但该方案无效——无论Debug还是Release模式,版权声明都会生成。以下是测试项目的相关代码:
项目文件(.csproj)内容
<?xml version="1.0" encoding="utf-8"?> <Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> <PropertyGroup> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> <ProjectGuid>{333D8793-BFF2-47E0-902D-FA2BC8D797C0}</ProjectGuid> <OutputType>Exe</OutputType> <RootNamespace>WithoutCopyExe</RootNamespace> <AssemblyName>WithoutCopyExe</AssemblyName> <TargetFrameworkVersion>v4.8</TargetFrameworkVersion> <FileAlignment>512</FileAlignment> <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> <Deterministic>true</Deterministic> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <PlatformTarget>AnyCPU</PlatformTarget> <DebugSymbols>true</DebugSymbols> <DebugType>full</DebugType> <Optimize>false</Optimize> <OutputPath>bin\Debug\</OutputPath> <DefineConstants>DEBUG;TRACE</DefineConstants> <ErrorReport>prompt</ErrorReport> <WarningLevel>4</WarningLevel> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> <PlatformTarget>AnyCPU</PlatformTarget> <DebugType>pdbonly</DebugType> <Optimize>true</Optimize> <OutputPath>bin\Release\</OutputPath> <DefineConstants>TRACE</DefineConstants> <ErrorReport>prompt</ErrorReport> <WarningLevel>4</WarningLevel> </PropertyGroup> <ItemGroup> <Reference Include="System" /> <Reference Include="System.Core" /> <Reference Include="System.Xml.Linq" /> <Reference Include="System.Data.DataSetExtensions" /> <Reference Include="Microsoft.CSharp" /> <Reference Include="System.Data" /> <Reference Include="System.Net.Http" /> <Reference Include="System.Xml" /> </ItemGroup> <ItemGroup> <Compile Include="Program.cs" /> <Compile Include="Properties\AssemblyCopyright.cs"> <DependentUpon>AssemblyCopyright.tt</DependentUpon> <DesignTime>True</DesignTime> <AutoGen>True</AutoGen> </Compile> <Compile Include="Properties\AssemblyInfo.cs" /> </ItemGroup> <ItemGroup> <None Include="App.config" /> </ItemGroup> <ItemGroup> <Service Include="{508349B6-6B84-4DF5-91F0-309BEEBAD82D}" /> </ItemGroup> <ItemGroup Condition=" '$(Configuration)' == 'Release' "> <Content Include="Properties\AssemblyCopyright.tt"> <Generator>TextTemplatingFileGenerator</Generator> <LastGenOutput>AssemblyCopyright.cs</LastGenOutput> </Content> </ItemGroup> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> </Project>
TT文件(AssemblyCopyright.tt)内容
<#@ template language="C#" #> // Generate copyright dynamically. using System; using System.Reflection; [assembly: AssemblyCopyright("Hurray @ <#=DateTime.Now.Year#>")]
AssemblyInfo.cs内容(未定义版权声明)
using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; // General Information about an assembly is controlled through the following // set of attributes. Change these attribute values to modify the information // associated with an assembly. [assembly: AssemblyTitle("CopyrightNotice")] [assembly: AssemblyDescription("")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("")] [assembly: AssemblyProduct("CopyrightNotice")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] // Setting ComVisible to false makes the types in this assembly not visible // to COM components. If you need to access a type in this assembly from // COM, set the ComVisible attribute to true on that type. [assembly: ComVisible(false)] // The following GUID is for the ID of the typelib if this project is exposed to COM [assembly: Guid("d6e52005-9be0-4571-b3fc-37c36c8c963b")] // Version information for an assembly consists of the following four values: // // Major Version // Minor Version // Build Number // Revision // // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] [assembly: AssemblyVersion("1.0.0.0")] [assembly: AssemblyFileVersion("1.0.0.0")]
内容的提问来源于stack exchange,提问作者Graviton
相关产品推荐
相关产品推荐

