ReferenceSwitcher is a .NET command line tool that automates switching between PackageReference and ProjectReference entries inside solutions. It is intended to make it easy to switch between developing against local projects and consuming NuGet packages.
The tool is published as a .NET tool (see the .csproj configuration). You can build and install it locally with:
dotnet build
# Then install the tool locally from the produced package if desiredThe tool exposes two main commands:
to-projects: replacesPackageReferenceentries withProjectReferenceentries when matching projects are found on disk.to-packages: replacesProjectReferenceentries withPackageReferenceentries when matching packages are found.
Both commands share the same basic arguments:
reference-switcher <command> \
--solution <path-to-sln> \
--scan-directory <directory-to-scan> \
[--add-projects-to-solution]--solution(-s): path to the.slnfile that defines the starting projects.--scan-directory(-d): directory that will be scanned recursively for.csprojfiles used to build the project index.--add-projects-to-solution: when specified, the tool also updates the solution file to reflect the changes.
When --add-projects-to-solution is used, the behavior depends on the selected command:
- After switching
PackageReferenceentries toProjectReference, the tool tracks all projects discovered via matchingPackageIdvalues. - Any discovered project that is not already present in the solution is added as a new
Project(...)entry in the.slnfile. - The new projects also receive entries in the
ProjectConfigurationPlatformssection so that they participate in the existing solution configurations (for exampleDebug|Any CPUandRelease|Any CPU).
If --add-projects-to-solution is not specified, only the .csproj files are updated and the solution file is left untouched.
- After switching
ProjectReferenceentries back toPackageReference, the tool looks for projects in the solution that are considered foreign and removes them from the.slnfile. - A project is considered foreign if its
.csprojpath is outside a reference root:- First, the tool walks up from the solution directory until it finds a directory that contains a
.gitfolder. That directory is used as the reference root. - If no Git repository is found, the parent directory of the solution file is used as the reference root instead.
- First, the tool walks up from the solution directory until it finds a directory that contains a
- Any project whose resolved absolute
.csprojpath is not under this reference root is treated as foreign. - For each foreign project, the corresponding
Project(...)/EndProjectblock is removed from the solution file, and any lines inGlobalsections that reference the project GUID (such asProjectConfigurationPlatformsor nested project mappings) are also removed.
If --add-projects-to-solution is not specified, only the .csproj files are updated and the solution file is left untouched.
- The tool relies on
PackageIdmetadata (and related MSBuild properties) to match projects with NuGet packages. - When multiple projects share the same
PackageId, a deterministic selection heuristic is used to pick one candidate, and a warning is written tostderr.