【问题标题】:User-defined condition variable for package referencing in C# .NET Core?用于 C# .NET Core 中包引用的用户定义条件变量?
【发布时间】:2020-12-11 21:31:09
【问题描述】:

我的 csproj 中有项目参考,如下所示:

 <ItemGroup>
   <ProjectReference Include="..\..\test\PressurePointLib\PressurePointLib.csproj" />   
  </ItemGroup>

它是一个库(DLL),仅用于包含在测试环境中,我不希望它链接到生产中的当前项目。

我希望根据条件变量有条件地包含此引用。

我知道我可以使用here 中描述的条件引用,但我想知道我是否可以定义自己的变量(比如 PressurePointsEnabled),如果可以,我如何在命令行和 Visual Studio 中设置该构建变量?

更新 我尝试了以下方法,它看起来有效。

 <ItemGroup Label="MyProject" Condition="'$(PressurePointsEnabled)'=='true'">
    <ProjectReference Include="..\..\test\PressurePointLib\BlackLine.Test.PressurePointLib.csproj" />
  </ItemGroup>

当我构建时

dotnet build -p:PressurePointsEnabled=true

@Berkay 与您下面的解决方案有何不同,它看起来更复杂?

【问题讨论】:

    标签: c# .net msbuild


    【解决方案1】:

    好的,试试这个,

    我创建了一个简单的console appclass library,并从powershell 使用dotnet build

    这是csproj文件,

    <Project Sdk="Microsoft.NET.Sdk">
        <PropertyGroup>
            <OutputType>Exe</OutputType>
            <TargetFramework>netcoreapp3.1</TargetFramework>
        </PropertyGroup>
    
        <ItemGroup>
            <PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
        </ItemGroup>
    
        <ItemGroup Condition=" $(PressurePointsEnabled.Contains('PROD')) ">
            <ProjectReference Include="..\ClassLibrary1\ClassLibrary1.csproj" />
        </ItemGroup>
    
        <Target Name="Test" AfterTargets="Build">
            <Message Text="Project $(ProjectName) Test PostBuild" Importance="high" />
            <Message Text="SUCCESS!" Condition=" $(PressurePointsEnabled.Contains('PROD')) " Importance="high" />
        </Target>
    </Project>
    

    正如你所说,我使用了PressurePointsEnabled。之后我在powershell 执行这个命令。如果条件为 PROD,我需要查看 SUCCESS!,否则不会出现构建后事件消息。

    这是命令;

    dotnet build -p:PressurePointsEnabled="UAT" C:\Users\Berkay\source\repos\ConsoleApp4
    

    输出:

    如果我将它的 UAT 更改为 PROD,输出将是:

    dotnet build -p:PressurePointsEnabled="UAT" C:\Users\Berkay\source\repos\ConsoleApp4
    

    所以,是的。您可以定义自己的常量并设置它的值,并从csproj 进行检查。

    【讨论】:

    • 我在上面尝试了一些东西(作为更新添加),它似乎有效。您能解释一下您的解决方案和我的解决方案之间的区别吗?
    • 解决方案是什么意思?你是说csproj文件吗?我已经全部粘贴了,你只需要 itemgroup 和 condition 部分。
    • 我的意思是你的答案不是“VS 解决方案”。看看我的更新,我需要做的就是让它工作
    • 是的,我的回答有点像 poc。试图解释并显示 2 个不同输入的结果。单行适合您。
    • 知道了。非常感谢,只是确保我没有遗漏任何重要的东西。 :)
    【解决方案2】:

    如果有人好奇,这是我的最终解决方案:

      <PropertyGroup Condition="'$(PressurePointsEnabled)'=='true'">
        <DefineConstants>PP_ENABLED</DefineConstants>
      </PropertyGroup>
    
     
    
      <ItemGroup Label="PPLib" Condition="$(DefineConstants.Contains('PP_ENABLED'))"> 
        <ProjectReference Include="..\..\test\PressurePointLib\MyService.Test.PressurePointLib.csproj" />
      </ItemGroup>
    

    你可以用

    dotnet build -p:PressurePointsEnabled=true
    

    如果为真,则定义 PP_ENABLED。

    在代码中你可以使用

    #if PP_ENABLED
    

    定义依赖于所包含库的代码。干杯。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-06
      • 1970-01-01
      • 1970-01-01
      • 2013-12-25
      • 2021-07-08
      • 2013-05-14
      • 2011-07-28
      相关资源
      最近更新 更多