你如何让T4ReferencePath工作?

时间:2014-06-09 21:39:09

标签: c# .net msbuild t4

我在这一天上撞了我的头几天。没有任何谷歌搜索或乱七八糟似乎得出答案。

我想在构建时运行T4模板。在其中,我需要访问我在此项目之前构建的程序集中的类型。我需要msbuild才能构建它,并且msbuild对VS变量没有好处,所以我需要使用其他方法来加载程序集。

我已经在很多地方读过T4ReferencePath是设置加载自定义程序集的地方的答案。但是,我无法让它发挥作用。

当我指定时:

<T4ReferencePath Include="$(TargetDir)" />

当我尝试在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.

我有#34; Visual Studio Visualization and Modeling SDK&#34;安装。

我为缺乏简洁而道歉,但我包括了我的测试项目的整个.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>

1 个答案:

答案 0 :(得分:3)

我终于偶然发现了答案。我无法想象之前没有人遇到过这种情况,但我们现在就是这样。希望,我已经开辟了一条能够帮助那些追随者的道路。

尽管常识告诉我,T4ReferencePath不是&#34;属性&#34;。它是&#34;项目&#34;。当我从&#34; PropertyGroup&#34;中删除它时并将其添加到&#34; ItemGroup&#34;,它很高兴。

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