用于在 C# .NET Core 中引用包的用户定义条件变量? [英] User-defined condition variable for package referencing in C# .NET Core?

查看:50
本文介绍了用于在 C# .NET Core 中引用包的用户定义条件变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 <ProjectReference Include=..\..\test\PressurePointLib\PressurePointLib.csproj";/></项目组>

它是一个库(DLL),只打算包含在测试环境中,我不希望它链接到生产中的当前项目.

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

我知道我可以按照

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

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

所以,是的.您可以定义自己的常量并设置其值,然后从 csproj 中检查.

I have project reference in my csproj something like the following:

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

It is a library(DLL) that is only meant to be included within test environment and I don't want it to be linked to the current project in production.

I want this reference to be included conditionally based on condition variable.

I understand I can use condition references as described here but I was wondering if I can define my own variable (say PressurePointsEnabled) and if so how do I set that build variable in command line and visual studio?

UPDATE I tried the following and it looks like it worked.

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

and when I build

dotnet build -p:PressurePointsEnabled=true

How is that different @Berkay from your solution below, it looks more complex?

解决方案

Okay try this,

I have created a simple console app and class library, and used dotnet build from powershell.

Here is the csproj file,

<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>

And as you said that I have used PressurePointsEnabled. After that I execute this command at powershell. I need to see SUCCESS! if condition is PROD otherwise post build event message won't be appear.

Here is the command;

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

The output:

And if I change it UAT to PROD, the output will be:

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

So, yes. You can define your own constant and set it's value, and check from csproj.

这篇关于用于在 C# .NET Core 中引用包的用户定义条件变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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