简体   繁体   中英

How to add project reference to another Project by using latest roslyn api

I am using Following Code but it did not work

MSBuildWorkspace workspace = MSBuildWorkspace.Create();
Microsoft.CodeAnalysis.Solution originalSolution = workspace.OpenSolutionAsync(sol.SolutionPath).Result;
Microsoft.CodeAnalysis.Solution newSolution = originalSolution;

ProjectReference pr = new ProjectReference(pid);
CodeAnalysis.Project prj = newSolution.Projects.Last();
prj = prj.AddProjectReference(pr);
newSolution = prj.Solution;
workspace.TryApplyChanges(newSolution);

MSBuildWorkspace just doesn't support propagating project references back to the project files when you call TryApplyChanges. I see you've filed the bug on CodePlex, but until that gets fixed (we're open source -- you can fix it too!) there's no workaround. If you only need to analyze the world as if that project reference exists, then you don't need to call that and can just use the Solution object you're trying to apply. If your goal is just to edit project files, another option is to use the MSBuild or XML APIs of your choice to directly manipulate the project files.

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