19

For user authorization, I only want to include a specific module for each user. So I configured Conditional Compilation like this

<DefineConstants>TRACE;DEBUG;SAMPLECONSTANT1</DefineConstants>

and edited the project file like this:

<ProjectReference Include="..\Solution1.Modules.Module1\Solution1.Modules.Module1.csproj" Condition="$(DefineConstants.Contains('SAMPLECONSTANT1'))">
  <Project>{4E378BD0-4FF8-4160-9331-1ECBFD2B6F30}</Project>
  <Name>Solution1.Modules.Module1</Name>
</ProjectReference>

For this case I want to add reference to project Module1 if DefineConstants contains SAMPLECONSTANT1; but no matter what I put in DefineConstants, the solution always loads the Module1 project. What did I do wrong here?

UPDATE: Actually my code is correct. Please see J0e3gan's answer. Visual Studio UI does not reflect conditional references within the References folder of a project. Therefore all references are visible in any given configuration or platform selection. The compiler and IntelliSense on the other hand are aware of conditional references, honoring the correct settings both with visual feedback and error notification during builds.

4

3 回答 3

28

我怀疑问题是您正在调节项目引用Module1而不是是否包含 Module1在解决方案中。

在解决方案中包含一个项目(并因此将其与解决方案一起加载)和一个在解决方案中引用另一个项目的项目当然是两件不同的事情。

更新:

如果您真的想调整项目参考,Joe Wrobel 写了一篇相关的博客文章,应该会有所帮助。ItemGroup关键要点是将包含ProjectReferenceto 条件的包装在一个Choose元素中 - 例如:

<Choose>
  <When Condition="$(DefineConstants.Contains('SAMPLECONSTANT1'))">
    <ItemGroup>
      <ProjectReference Include="..\Solution1.Modules.Module1\Solution1.Modules.Module1.csproj">
        <Project>{4E378BD0-4FF8-4160-9331-1ECBFD2B6F30}</Project>
        <Name>Solution1.Modules.Module1</Name>
      </ProjectReference>
      <!-- other ProjectReference elements -->
    </ItemGroup>
  </When>
  <Otherwise>
    <ItemGroup>
      <!-- other ProjectReference elements -->
    </ItemGroup>
  </Otherwise>
</Choose>

从我今晚的测试来看,这非常适合将项目引用设置为是否SAMPLECONSTANT1定义了类似的常量。但是,请注意,条件项目引用不会显示在(可能)引用项目的 References 文件夹下的解决方案资源管理器中 -无论是否定义了条件常量。

为了看到条件作用有效,我必须构建: withSAMPLECONSTANT1 defined,引用项目成功构建,同时使用 - 中定义的类,Module1如预期的那样;并且没有SAMPLECONSTANT1定义,引用项目无法构建,因为Module1无法解析中定义的类 - 也如预期的那样。

于 2013-05-15T04:46:47.203 回答
1

接受的答案对我不起作用。但是,我并不暗示这是不正确的。对我有用的是:

<Import Project="..\MnM.GWS.Chip2\MnM.GWS.Chip1.projitems" Label="Shared" Condition="$(DefineConstants.Contains('Chip1'))" />
<Import Project="..\MnM.GWS.Chip2\MnM.GWS.Chip2.projitems" Label="Shared" Condition="$(DefineConstants.Contains('Chip2'))" />

更改常量时我没有收到任何错误。

于 2019-11-23T16:30:22.087 回答
0

如果要使用 DEBUG 常量,请确保在项目属性中启用它(Properteis > Build > Define DEBUG Constant)。

基于已接受答案的示例:

  <Choose>
    <When Condition="$(DefineConstants.Contains('DEBUG'))">
      <ItemGroup>
        <ProjectReference Include="..\..\..\..\..\MyLocalProjectPath.csproj" />
      </ItemGroup>
    </When>
    <Otherwise>
      <ItemGroup>
        <PackageReference Include="MyNugetPackage" Version="2.1.4" />
      </ItemGroup>
    </Otherwise>
  </Choose>
于 2021-03-17T13:37:41.000 回答