【问题标题】:Conditional references in .NET project, possible to get rid of warning?.NET 项目中的条件引用,可以摆脱警告吗?
【发布时间】:2011-02-04 17:44:01
【问题描述】:

我有两个对 SQLite 程序集的引用,一个用于 32 位,一个用于 64 位,看起来像这样(这是一个尝试摆脱警告的测试项目,不要挂断电话路径):

<Reference Condition=" '$(Platform)' == 'x64' " Include="System.Data.SQLite, Version=1.0.61.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139, processorArchitecture=AMD64">
  <SpecificVersion>True</SpecificVersion>
  <HintPath>..\..\LVK Libraries\SQLite3\version_1.0.65.0\64-bit\System.Data.SQLite.DLL</HintPath>
</Reference>
<Reference Condition=" '$(Platform)' == 'x86' " Include="System.Data.SQLite, Version=1.0.65.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139, processorArchitecture=x86">
  <SpecificVersion>True</SpecificVersion>
  <HintPath>..\..\LVK Libraries\SQLite3\version_1.0.65.0\32-bit\System.Data.SQLite.DLL</HintPath>
</Reference>

这会产生以下警告:

Warning 1 The referenced component 'System.Data.SQLite' could not be found.     

我可以摆脱这个警告吗?

我研究过的一种方法是在开发时将我的项目配置为 32 位,并让构建机器在构建 64 位时修复引用,但这似乎有点尴尬并且可能容易出现错误。

还有其他选择吗?

我想摆脱它的原因是警告显然被 TeamCity 接收并定期标记为我需要调查的东西,所以我想完全摆脱它。


编辑:根据答案,我试过这个:

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
    ...
    <SqlitePath>..\..\LVK Libraries\SQLite3\version_1.0.65.0\32-bit</SqlitePath>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
    ...
    <SqlitePath>..\..\LVK Libraries\SQLite3\version_1.0.65.0\32-bit</SqlitePath>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x64' ">
    ...
    <SqlitePath>..\..\LVK Libraries\SQLite3\version_1.0.65.0\64-bit</SqlitePath>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x64' ">
    ...
    <SqlitePath>..\..\LVK Libraries\SQLite3\version_1.0.65.0\64-bit</SqlitePath>
</PropertyGroup>

然后在我的参考中:

<Reference Include="System.Data.SQLite">
    <SpecificVersion>False</SpecificVersion>
    <HintPath>$(SqlitePath)\System.Data.SQLite.DLL</HintPath>
</Reference>

这消除了警告,但它正确吗?

【问题讨论】:

    标签: c# reference project conditional compiler-warnings


    【解决方案1】:

    如果没有适用于 SQL Lite 的“AnyCPU”程序集,您将无法使用单独的构建。

    要进行单独的构建,请在条件属性组中创建一个提供正确路径的属性,然后使用该属性来拥有单个引用(即将条件移到引用项组之外)。有一个使用此类属性的示例(用于自定义 FXCop 扩展)here,您可以看到在.csproj 文件的开头定义了许多条件属性。

    (总结:VS 无法处理 MSBuild 的所有可能性。)

    【讨论】:

    • 我已经用我认为正确的方式编辑了我的答案,你能看看它并告诉我这是否是你的意思吗?
    【解决方案2】:

    在我看来,您的原始项目的问题在于您有 &lt;SpecificVersion&gt;True&lt;/SpecificVersion&gt; 指定 System.Data.SQLite, Version=1.0.61.0,而实际的程序集是版本 1.0.65。修复 Reference 中的程序集名称中的版本应该会有所帮助。

    【讨论】:

    • 嗯,你说得有道理,尽管它会为 x86 和 x64 生成警告,其中一个显然是正确的。我会调查的。
    • 显然我的文件有问题,在那种情况下文件实际上是 1.0.61。该引用是通过 Visual Studio 手动添加的,因此应该是正确的。我会修复文件,但这不是警告的罪魁祸首。
    • 另外两个建议——将Condition 推到ItemGroup 上,而不是Reference 本身;尝试绝对路径。
    • 将 Condition 放入 ItemGroup 并没有帮助,以防万一有人遇到这种情况。 Visual Studio 愚蠢地认为您仍然对程序集有引用,即使您的条件评估为 false。
    猜你喜欢
    • 2014-12-29
    • 1970-01-01
    • 1970-01-01
    • 2018-03-12
    • 2015-11-16
    • 2016-01-15
    • 1970-01-01
    • 1970-01-01
    • 2012-06-23
    相关资源
    最近更新 更多