Visual Studio Community 2017中的条件引用 [英] Conditional reference in Visual Studio Community 2017

查看:137
本文介绍了Visual Studio Community 2017中的条件引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个多平台应用程序.我有一个多目标共享库(目标是.netstandard 2.0和.net 4.5)...请参阅项目文件:

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:

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

当我在Windows上的Visual Studio 2017中构建项目时,我在输出中得到两个目录(netstandard2.0,net45)和相应的dll.构建成功.

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. The build is a success.

当我在Mac上的Visual Studio 2017中构建完全相同的项目(相同的代码)时,会出现这种性质的错误:

When I build the exact same project (same code) in visual studio 2017 on a mac, I get errors of this nature:

'CommandLine.DotNetStandard,Version = 1.0.30'和'CommandLine,Version = 1.9.71.2'中都存在类型'OptionAttribute'

The type 'OptionAttribute' exists in both 'CommandLine.DotNetStandard, Version=1.0.30' and '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>

这对于Windows很好用,但是在Mac上似乎没有观察到这种情况.这是Mac上Visual Studio的已知错误吗?我在做错什么吗?

This works great for windows, but on the mac it appears it is not observing the condition. Is this a known bug for visual studio on mac? Am I doing something wrong?

推荐答案

在这些情况下,Visual Studio会忽略该条件.请使用Choose/When,它应该得到完全支持: https://msdn. microsoft.com/en-us/library/ms164282.aspx

Visual Studio ignores the condition in these cases. Use a Choose/When instead, that should be fully supported: https://msdn.microsoft.com/en-us/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>

这篇关于Visual Studio Community 2017中的条件引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆