你如何让 T4ReferencePath 工作?

Jam*_*mie 4 .net c# msbuild t4

在这个问题上,我已经用头撞墙了几天。没有多少谷歌搜索或乱七八糟的东西似乎会产生答案。

我想在构建时运行 T4 模板。在其中,我需要访问我在此项目之前构建的程序集中的类型。我需要 msbuild 才能构建它,而 msbuild 不能很好地处理 VS 变量,所以我需要使用另一种方法来加载程序集。

我在很多地方都读到 T4ReferencePath 是设置加载自定义程序集的位置的答案。但是,我无法让它工作。

当我指定这个时:

<T4ReferencePath Include="$(TargetDir)" />
Run Code Online (Sandbox Code Playgroud)

当我尝试在 VS 中加载项目或使用 msbuild 运行它时,我得到了这个:

d:\Users\250894\Documents\Visual Studio 2013\Projects\TestT4\TestT4.csproj(90,22): error MSB4066: The attribute "Include" in element <T4ReferencePath> is unrecognized.
Run Code Online (Sandbox Code Playgroud)

我确实安装了“Visual Studio Visualization and Modeling SDK”。

我为不够简洁而道歉,但我包含了我的测试项目的整个 .csproj 文件,以防我遗漏了一些显而易见的东西。我不包括我的 .tt 文件,因为如果我什至无法加载我的 csproj,它就无关紧要。如果我排除 T4ReferencePath,则一切正常。

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" 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>{5B9944E7-47CF-4BFE-BAEF-F02D29D59E80}</ProjectGuid>
    <OutputType>Exe</OutputType>
    <AppDesignerFolder>Properties</AppDesignerFolder>
    <RootNamespace>TestT4</RootNamespace>
    <AssemblyName>TestT4</AssemblyName>
    <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
    <FileAlignment>512</FileAlignment>
  </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>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
    <DebugSymbols>true</DebugSymbols>
    <OutputPath>bin\x64\Debug\</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <DebugType>full</DebugType>
    <PlatformTarget>x64</PlatformTarget>
    <ErrorReport>prompt</ErrorReport>
    <CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
    <Prefer32Bit>true</Prefer32Bit>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
    <OutputPath>bin\x64\Release\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <Optimize>true</Optimize>
    <DebugType>pdbonly</DebugType>
    <PlatformTarget>x64</PlatformTarget>
    <ErrorReport>prompt</ErrorReport>
    <CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
    <Prefer32Bit>true</Prefer32Bit>
  </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.Xml" />
  </ItemGroup>
  <ItemGroup>
    <Compile Include="Program.cs" />
    <Compile Include="Properties\AssemblyInfo.cs" />
    <Compile Include="Test.cs">
      <AutoGen>True</AutoGen>
      <DesignTime>True</DesignTime>
      <DependentUpon>Test.tt</DependentUpon>
    </Compile>
  </ItemGroup>
  <ItemGroup>
    <None Include="App.config" />
    <None Include="Test.tt">
      <Generator>TextTemplatingFileGenerator</Generator>
      <LastGenOutput>Test.cs</LastGenOutput>
    </None>
  </ItemGroup>
  <ItemGroup>
    <Service Include="{508349B6-6B84-4DF5-91F0-309BEEBAD82D}" />
  </ItemGroup>
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
  <PropertyGroup>
    <!-- Get the Visual Studio version – defaults to 12: -->
    <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">12.0</VisualStudioVersion>
    <!-- Keep the next element all on one line: -->
    <VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
    <TransformOnBuild>true</TransformOnBuild>
    <IncludeDslT4Settings>true</IncludeDslT4Settings>
    <T4ReferencePath Include="$(TargetDir)" />
  </PropertyGroup>
  <Import Project="$(VSToolsPath)\TextTemplating\Microsoft.TextTemplating.targets" />
</Project>
Run Code Online (Sandbox Code Playgroud)

Jam*_*mie 6

我终于偶然发现了答案。我无法想象以前没有人遇到过这种情况,但我们来了。希望我开辟了一条可以帮助后来者的道路。

尽管常识告诉我,T4ReferencePath 不是“属性”。这是一个“物品”。当我将它从“PropertyGroup”中删除并将其添加到“ItemGroup”时,它变得很高兴。

一般而言,软件开发和计算机使用的障碍之一是无用的错误消息。我不能说这是我遇到过的最令人震惊的例子,但它就在那里。