I am trying to remove one reference and replace it with new reference for multiple C# projects.
How do I add new reference using microsoft.build.evaluation
using Microsoft.Build.Evaluation;
var projCollection = new ProjectCollection();
var proj = projCollection.LoadProject(csproj_filepath);
var items = proj.GetItems("Reference");
foreach(var item in items)
{
if(item. EvaluatedInclude.Equals("XYZ_reference"))
{
//remove this reference
// add new reference with new hint path etc
}
}
proj.Save();
There is documentation here, but not very useful in terms of difference between with and without metadata include or usage example.
I do not quite understand what exactly the problem is. For ex: small snippet from my old project for replacing
PackageReference
withProjectReference
for easier debugging locally:In fact, it all comes down to finding the needed reference and replacing the attributes. After saving, Visual Studio detected the changes and requested to reload the project, after which the changes were applied.