简体   繁体   English

基于解决方案配置的条件 ProjectReference

[英]Conditional ProjectReference based on solution configuration

Is it possible to setup ProjectReference's to be conditionally included based on whatever the project is loaded in the solution?是否可以根据解决方案中加载的项目有条件地包含 ProjectReference? We have a plugin system which scans all the assemblies within the application for plugin entry point under debugger.我们有一个插件系统,它在调试器下扫描应用程序中的所有程序集以查找插件入口点。 This is pretty handy on development cycle so we can include the plugin projects in the same solution and easily debug both the application and plugin code.这在开发周期中非常方便,因此我们可以将插件项目包含在同一解决方案中,并轻松调试应用程序和插件代码。

However, this requires all of the plugins to be references in the main application using ProjectReference's (with the condition to do this only on debug configuration) and forces that every plugins project to be loaded in order to allow building inside Visual Studio.但是,这要求所有插件都是使用 ProjectReference 的主应用程序中的引用(条件是仅在调试配置时执行此操作)并强制加载每个插件项目以允许在 Visual Studio 中构建。 It would be helpful if you could unload the plugin projects freely without needing to touch the main projects ProjectReference's to improve solution open time.如果您可以自由卸载插件项目而无需触及 ProjectReference 的主要项目以改进解决方案的打开时间,这将很有帮助。

I can't seem to find any documentation on which MSBuild property the solution configuration is carried over that could be used as a new condition.我似乎找不到任何关于解决方案配置的 MSBuild 属性可以用作新条件的文档。

UPDATE: The diagnostic error code that is reported for unloaded projects is NU1105.更新:为卸载的项目报告的诊断错误代码是 NU1105。 So I started digging through where this is reported from and came across VsSolutionRestoreService.cs .因此,我开始深入研究报告的来源,并发现了VsSolutionRestoreService.cs From the looks of it, it reads directly from VisualStudio and may not even be part of MSBuild and ultimately there is no way of doing this with single solution file.从它的外观来看,它直接从 VisualStudio 读取,甚至可能不是 MSBuild 的一部分,最终无法使用单个解决方案文件执行此操作。

ProjectReference Conditional on Solution Configuration以解决方案配置为条件的 ProjectReference

It helps to understand that layered on the base build engine is a C# build framework.它有助于理解在基础构建引擎上分层的是 C# 构建框架。 When you use Visual Studio or the dotnet tool to create a project, the resulting project imports a number of other MSBuild files.当您使用 Visual Studio 或dotnet工具创建项目时,生成的项目会导入许多其他 MSBuild 文件。

In terms of the base build engine, ProjectReference is just an ItemGroup .就基本构建引擎而言, ProjectReference只是一个ItemGroup The special meaning of ProjectReference as a set of projects the current project depends on, is implemented in the imported files. ProjectReference作为当前项目所依赖的一组项目的特殊含义,是在导入的文件中实现的。

ProjectReference is an Item element and Items support the Condition attribute. ProjectReference是一个Item元素,Items 支持Condition属性。 Yes, a ProjectReference can be conditional.是的, ProjectReference可以是有条件的。

The current configuration is available in a property named Configuration .当前配置在名为Configuration的属性中可用。 The Configuration property is documented in " Common MSBuild project properties ". Configuration属性记录在“ Common MSBuild project properties ”中。

  <ItemGroup>
    <ProjectReference Include="..\..\some\other\project.csproj" Condition="'$(Configuration)' == 'Debug'" />
  </ItemGroup>

In Visual Studio in the 'Build' menu, choose 'Configuration Manager...' to see how the solution configuration is mapped to project configurations.在 Visual Studio 的“构建”菜单中,选择“配置管理器...”以查看解决方案配置如何映射到项目配置。 Unless it has been customized, building the solution in Debug will build each project of the solution in Debug.除非已自定义,否则在 Debug 中构建解决方案将在 Debug 中构建解决方案的每个项目。

ProjectReference Conditional on Projects in the Solution ProjectReference 以解决方案中的项目为条件

There is a set of properties with information about the solution: SolutionDir , SolutionExt , SolutionFileName , SolutionName , and SolutionPath .有一组包含解决方案信息的属性: SolutionDirSolutionExtSolutionFileNameSolutionNameSolutionPath These properties are documented in the C++ documentation in " Common macros for MSBuild commands and properties ".这些属性记录在“ MSBuild 命令和属性的通用宏”中的 C++ 文档中。 That is the extent of the information provided to a project about a solution.这是提供给项目的有关解决方案的信息的范围。 And in Legacy style projects these properties will be undefined when a project is built directly and not via the solution.在传统风格的项目中,当直接而不是通过解决方案构建项目时,这些属性将是未定义的。

When a project is unloaded in the Visual Studio solution explorer window, the solution file is not changed.在 Visual Studio 解决方案资源管理器窗口中卸载项目时,解决方案文件不会更改。 The loaded or unloaded state of a project is not part of the solution file.项目的加载或卸载状态不是解决方案文件的一部分。 It's stored elsewhere by the IDE.它由 IDE 存储在别处。

Instead of unloading projects, you could create a set of solution filter files.您可以创建一组解决方案过滤器文件,而不是卸载项目。 The solution filter file is JSON that lists the 'loaded' projects.解决方案过滤器文件是列出“已加载”项目的 JSON。 See " Filtered solutions in Visual Studio ".请参阅“ Visual Studio 中的筛选解决方案”。 Unfortunately the Solution* properties will still be defined for the underlying solution and won't give you the solution filter file that is in use and there are no properties for the solution filter file.不幸的是, Solution*属性仍将为基础解决方案定义,不会为您提供正在使用的解决方案过滤器文件,并且解决方案过滤器文件没有任何属性。

You could create different Configurations in the solution file with different sets of projects disabled for build.您可以在解决方案文件中创建不同的配置,并为构建禁用不同的项目集。 In addition to 'Debug' you might add 'Debug-Plugin1', 'Debug-Plugin2', and so on.除了“Debug”之外,您还可以添加“Debug-Plugin1”、“Debug-Plugin2”等。 You could write a custom MSBuild Task that reads the solution file and for the current configuration returns a list of enabled projects.您可以编写一个自定义 MSBuild 任务来读取解决方案文件,并针对当前配置返回已启用项目的列表。 The custom Task could use the Microsoft.Build package to read and parse the solution file.自定义任务可以使用Microsoft.Build包来读取和解析解决方案文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM