无法生成包含C#6.0代码的MSBuild命令行

本文关键字:代码 MSBuild 命令行 C#6 包含 | 更新日期: 2023-09-27 18:28:29

我在获取MSBuild编译c#6.0代码时遇到问题,它在尝试构建表达式主体语法etx时报告失败。

所以我多年来一直在创建这样的构建项目。我有一个powershell脚本,它启动了带有一些参数的msbuild:

msbuild.exe _build'build.proj
        /p:Build_Number=1.2.0
        /p:Configuration=QA
        /p:SolutionName=MPGCS-Api.sln
        /ToolsVersion:14.0

我验证了msbuild.exe来自C:''Program Files(x86)''msbuild''14.0''Bin,我通过进入该文件夹并指定.proj文件的完整物理路径来做到这一点。我也有同样的错误。

我一直在寻找一个关于如何使用C#6.0设置MSBuild项目文件的例子,但没有成功,所以这是我的基本设置(这是build.proj文件)。如果我不得不猜测我没有导入正确的目标,等等。但我有点迷失了方向。这是我的项目文件:

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)'$(MSBuildToolsVersion)'Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)'$(MSBuildToolsVersion)'Microsoft.Common.props')" />
  <PropertyGroup>
    <Root>$(MSBuildStartupDirectory)</Root>
    <NugetExe>$(Root)'_build'lib'nuget'nuget.exe</NugetExe>
    <Build_Number>0.0.0</Build_Number>
    <SolutionName></SolutionName>
    <TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
    <VisualStudioVersion>14.0</VisualStudioVersion>
  </PropertyGroup>
  <Target Name="Clean">
    <!-- Clean up -->
    <ItemGroup>
      <FilesToDelete Include="$(Root)'_build'Artifacts'**'*.*" />
      <FilesToDelete Include="$(Root)'_build'Published'**'*.*" />
    </ItemGroup>
    <Delete Files="@(FilesToDelete)" ContinueOnError="false" />
    <!-- Ensure directories exists -->
    <MakeDir Directories="$(MSBuildProjectDirectory)'Artifacts" Condition="!Exists('$(MSBuildProjectDirectory)'Artifacts')" />
    <MakeDir Directories="$(MSBuildProjectDirectory)'Published" Condition="!Exists('$(MSBuildProjectDirectory)'Artifacts')" />
  </Target>
  <Target Name="Debug" AfterTargets="Clean">
    <!-- Diagnostics -->
    <Message Text="Diagnostics:"/>
    <Message Text="Build Number:    $(build_number)" />
    <Message Text="Configuration:    $(Configuration)" />
    <Message Text="VisualStudioVersion:    $(VisualStudioVersion)" />
    <Message Text="Project root:    $(Root)" />
    <!-- Restore Nuget Packages -->
    <Message Text="Restoring nuget..."/>
    <Exec Command="$(NugetExe) restore $(Root)'$(SolutionName)" />
  </Target>
  <Target Name="GenerateOctopackAPI" AfterTargets="Debug">
    <ItemGroup>
      <ProjectToBuild Include="$(Root)'MPG.CS.Api'MPG.CS.Api.csproj" />
    </ItemGroup>
    <MSBuild Projects="@(ProjectToBuild)" ContinueOnError="false" Targets="Rebuild" Properties="
      RunOctoPack=true;
      Configuration=$(ProjectBuildMode);
      Platform=AnyCpu;
      TargetFrameworkVersion=$(TargetFrameworkVersion);
      VisualStudioVersion=$(VisualStudioVersion);
      OctoPackPublishPackageToFileShare=$(Root)'_build'Artifacts;
      OctoPackPackageVersion=$(Build_Number);
      OctoPackProjectName=UI;
      OutputPath=bin'$(ProjectBuildMode)" />
    </Target>
</Project>

以下是我的"调试"目标中的一些信息,正如您所看到的,我正在根据在线阅读的一些建议将VisualStudioVersion设置为14.0

 Diagnostics:
  Build Number:    1.2.0
  Configuration:    QA
  VisualStudioVersion:    14.0
  Project root:    C:'dev'mpg'MPGCS-Api

这里有一个错误示例,它在c#6.0代码上基本上失败了,如果我删除c#6代码,所有内容都会编译:

TicketType.cs(19,28): error CS1002: ; expected [C:'dev'mpg'MPGCS-Api'MPG.CS.Model'MPG.CS.Model.csproj]
  TicketType.cs(19,44): error CS1519: Invalid token '(' in class, struct, or interface member declaration [C:'dev'mpg'MPGCS-Api'MPG.CS.Model'MPG.CS.Model.cs proj]
  TicketStatus.cs(16,36): error CS1002: ; expected [C:'dev'mpg'MPGCS-Api'MPG.CS.Model'MPG.CS.Model.csproj]
  TicketStatus.cs(16,53): error CS1519: Invalid token '(' in class, struct, or interface member declaration [C:'dev'mpg'MPGCS-Api'MPG.CS.Model'MPG.CS.Model. csproj]
  TicketStatus.cs(16,82): error CS1519: Invalid token '(' in class, struct, or interface member declaration [C:'dev'mpg'MPGCS-Api'MPG.CS.Model'MPG.CS.Model. csproj]

这里有一行它失败了(C#代码):

public bool IsAbbreviated  => (Title.ToLower() == "open" || Title.ToLower() == "closed");

我不知所措,我想只要使用正确的msbuild.exe就能让我利用c#6.0。如有任何帮助,我们将不胜感激。

无法生成包含C#6.0代码的MSBuild命令行

如果您在独立于开发环境的构建服务器上运行powershell脚本,则需要确保机器本身能够处理C#6.0。由于它同时使用了不同的编译器,如果不安装VS2015,它就无法工作。或者,您可以添加Microsoft.Net.Compilers nuget包作为依赖项,以允许VS 2012和VS 2013对其进行编译。