Hello I ran into a XY problem using Visual Studio 2017 and dotnet 6.0.100
My original problem is: "How to add project dependencies in a MSBuild solution programmatically with C#"
This is needed to force a modular application to compile unreferenced projects in a .sln
during the build process to also compile all other projects in the solution.
This .sln
file is built programmatically and should also do this step automatically during creation of the .sln
to reduce possible problems for the user working with this .sln
.
The .csproj
of the projects however should not be touched since they are part of a git submodule, that should not include these changes to the .csproj
but only the changes to the .csproj
made by the user.
Solution
There is a 10 year old solution described here and here using the DTE Interface.
Derived problem: I failed to include the correct references for the EnvDTE.DTE or EnvDTE80.DTE2 objects to be available in code.
I tried adding the Microsoft.VisualStudio.Setup.Configuration.Interop
NuGet package and searched in the references for EnvDTE
or Microsoft.VisualStudio.Shell
as seen e.g. in the example here without luck.
Original problem
Manually the project dependencies can be added as described here
I tried adding the project dependencies via the dotnet sln
command but didn't find a corresponding command to add the dependencies.
dotnet sln -h
only points to add, list and remove. The add command didn't show an option for dependencies.
Since I already use dotnet commands to create the solution and add projects this would be a good solution for me if there is a way.
The other thing I tried was using Microsoft.Build.Construction
:
SolutionFile solutionFile = SolutionFile.Parse(PROJECT_SOLUTION_FILE.FullName);
var projectInSolution = solutionFile.ProjectsInOrder;
which does enable me to read the projects in the sln but seemingly not to manipulate it since its all readonly.
Summary
I need a way to tell the solution that all projects have to be compiled on run/compilation of the designated one.
- To accomplish this I either need a way to add project dependencies to the .sln file via
- dotnet shell commands
- something like Microsoft.Build.Construction
- or a good source explaining to me how I can get the EnvDTE code running in my MS Visual Studio 2017
- or there is some awesome way to do this that I didn't stumble across yet
I really appreciate any help to solving my problem.
Edit:
To clarify project dependencies are different from project references in .csproj
files!
The project dependencies I refer to are this section in the .sln
:
Project("{GUID}") = "PROJECTNAME", "CSPROJECT_LOCATION", "{GUID}"
ProjectSection(ProjectDependencies) = postProject
{GUID} = {GUID}
{GUID} = {GUID}
...
EndProjectSection
EndProject