简体   繁体   English

Visual Studio Community 2017中的条件引用

[英]Conditional reference in Visual Studio Community 2017

I am creating a multi-platform application. 我正在创建一个多平台应用程序。 I have a multi-targeted shared library (targeting .netstandard 2.0 and .net 4.5)...See project file: 我有一个多目标共享库(目标是.netstandard 2.0和.net 4.5)...请参阅项目文件:

  <PropertyGroup>
    <TargetFrameworks>netstandard2.0;net45</TargetFrameworks>
  </PropertyGroup>

When I build the project in visual studio 2017 on windows, I get two directories in the output (netstandard2.0, net45) and the corresponding dlls. 当我在Windows上的Visual Studio 2017中构建项目时,我在输出中得到两个目录(netstandard2.0,net45)和相应的dll。 The build is a success. 构建成功。

When I build the exact same project (same code) in visual studio 2017 on a mac, I get errors of this nature: 当我在Mac上的Visual Studio 2017中构建完全相同的项目(相同的代码)时,出现这种错误:

The type 'OptionAttribute' exists in both 'CommandLine.DotNetStandard, Version=1.0.30' and 'CommandLine, Version=1.9.71.2' 类型'OptionAttribute'在'CommandLine.DotNetStandard,Version = 1.0.30'和'CommandLine,Version = 1.9.71.2'中都存在

I conditionally referenced a command line parser library in the following way: 我通过以下方式有条件地引用了命令行解析器库:

  <!-- CommandLineParser library -->
  <ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
    <PackageReference Include="CommandLine.DotNetStandard">
      <Version>1.0.3</Version>
    </PackageReference>
  </ItemGroup>

  <ItemGroup Condition="'$(TargetFramework)' == 'net45'">
    <PackageReference Include="CommandLineParser">
      <Version>1.9.71</Version>
    </PackageReference>
  </ItemGroup>

This works great for windows, but on the mac it appears it is not observing the condition. 这对于Windows非常有用,但是在Mac上似乎没有观察到这种情况。 Is this a known bug for visual studio on mac? 这是Mac上Visual Studio的已知错误吗? Am I doing something wrong? 难道我做错了什么?

Visual Studio ignores the condition in these cases. 在这些情况下,Visual Studio会忽略该条件。 Use a Choose/When instead, that should be fully supported: https://msdn.microsoft.com/en-us/library/ms164282.aspx 使用Choose/When代替,应该完全支持: https//msdn.microsoft.com/zh-cn/library/ms164282.aspx

<Choose> 
  <When Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
    <ItemGroup>
      <PackageReference Include="CommandLine.DotNetStandard">
        <Version>1.0.3</Version>
      </PackageReference>
    </ItemGroup>
  </When>
  <When Condition=" '$(TargetFramework)' == 'net45' ">
    <ItemGroup> 
      <PackageReference Include="CommandLineParser">
        <Version>1.9.71</Version>
      </PackageReference>
    </ItemGroup>
  </When>
</Choose>

If MsBuild is taking into account only your first <Choose/> or condition then you'd want to do this: 如果MsBuild仅考虑您的第一个<Choose/>或条件,则您需要这样做:

 <Choose>
    <When Condition="'$(Configuration)'=='Debug'">
      <ItemGroup>
        <ProjectReference Include="..\path\to_your_project.csproj" />
      </ItemGroup>
    </When>
    <Otherwise>
      <ItemGroup>
        <PackageReference Include="Package-Name" Version="1.0.0"/>
      </ItemGroup>
    </Otherwise>
  </Choose>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM