简体   繁体   中英

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

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.

UPDATE: The diagnostic error code that is reported for unloaded projects is NU1105. So I started digging through where this is reported from and came across 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.

ProjectReference Conditional on Solution Configuration

It helps to understand that layered on the base build engine is a C# build framework. When you use Visual Studio or the dotnet tool to create a project, the resulting project imports a number of other MSBuild files.

In terms of the base build engine, ProjectReference is just an ItemGroup . The special meaning of ProjectReference as a set of projects the current project depends on, is implemented in the imported files.

ProjectReference is an Item element and Items support the Condition attribute. Yes, a ProjectReference can be conditional.

The current configuration is available in a property named Configuration . The Configuration property is documented in " 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. Unless it has been customized, building the solution in Debug will build each project of the solution in Debug.

ProjectReference Conditional on Projects in the Solution

There is a set of properties with information about the solution: SolutionDir , SolutionExt , SolutionFileName , SolutionName , and SolutionPath . These properties are documented in the C++ documentation in " Common macros for MSBuild commands and properties ". 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. The loaded or unloaded state of a project is not part of the solution file. It's stored elsewhere by the 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. See " Filtered solutions in 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.

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. You could write a custom MSBuild Task that reads the solution file and for the current configuration returns a list of enabled projects. The custom Task could use the Microsoft.Build package to read and parse the solution file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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