是否可以引用解决方案中存在的项目,如果不在.NET Core中,可以使用NuGet程序包引用作为后备吗? [英] Is it possible to reference a project that exists in the solution and use NuGet package reference as fall-back if not in .NET Core?

查看:155
本文介绍了是否可以引用解决方案中存在的项目,如果不在.NET Core中,可以使用NuGet程序包引用作为后备吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.NET Standard项目,其中为ASP.NET Core CMS框架实现了一个模块。当前,它使用来自NuGet包的CMS框架库。如果我从GitHub上获取CMS框架的源代码,并将模块添加到其解决方案中,然后将包引用替换为实际的项目引用,它将正常工作。

I have an .NET Standard project where I implemented a module for an ASP.NET Core CMS framework. Currently, it uses the CMS framework libraries coming from NuGet packages. If I grab the source code of the CMS framework from GitHub and add my module to its solution and replace the package references to the actual project references it will work fine.

我的目标是在不更新csproj文件中的引用的情况下使其正常工作,因此,如果将项目添加到完整的源代码解决方案中,则使用项目引用,否则使用NuGet包引用。

My goal is to make it work without updating the references in the csproj file so if the project is added to the full source code solution then use the project references, otherwise use the NuGet package references.

因此,假设.NET Standard项目称为 ModuleA。它具有对 ModuleB的软件包引用:

So let's say the .NET Standard project is called 'ModuleA'. It has a package reference to 'ModuleB':

<ItemGroup>
  <PackageReference Include="ModuleB" Version="1.0.0" />
</ItemGroup>

当我想在ModuleB可以访问的解决方案中使用ModuleA时,我使用对它的项目引用:

When I want to use ModuleA in a solution where ModuleB is accessible then I use project reference to it:

<ItemGroup>
  <ProjectReference Include="..\..\ModuleB\ModuleB.csproj" />
</ItemGroup>

我想以某种方式将它们都包含在.csproj文件中,并使其使用正确的构建参考(例如,基于某些类似项目的条件吗?)。

I'd like to include both of them in the .csproj file somehow and make it to use the proper references on build (e.g. based on some conditions like project exists?).

如果将两者都添加到csproj中,则构建将失败(例如,无法找到项目... ModuleB.csproj。检查项目引用是否有效以及项目文件是否存在。')。

If both are added to the csproj then the build will fail (e.g. 'Unable to find project ...ModuleB.csproj. Check that the project reference is valid and that project file exists.').

推荐答案

可能是动态地执行此操作,但是我认为它的工作原理并不能完全透明。

You could do that probably dynamically, but I think it's not really transparant how that would work.

我建议添加一个配置,例如在我的示例 Local-Debug中,并在csproj中使用条件。

I would recommend to add a configuration, e.g. in my example "Local-Debug" and use conditions in your csproj.

创建配置:

在csproj中,您可以执行以下操作:

And in your csproj you could do this:

<ItemGroup>
  <ProjectReference Condition="'$(Configuration)' == 'Local-Debug'" Include="otherProject.csproj" />
  <PackageReference Condition="'$(Configuration)' != 'Local-Debug'" Include="otherProjectPackage" Version="2.4.1" />
</ItemGroup>

这篇关于是否可以引用解决方案中存在的项目,如果不在.NET Core中,可以使用NuGet程序包引用作为后备吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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