Reputation: 34
I have a solution where I have a Shared Project of extension ".shproj"
I need to add a class to the project with Roslyn, but when I look in the list of projects, It does not contain the shared project:
const string solutionTargetPath = @"C:\Repo\Solution.sln";
//Roslyn startup
MSBuildLocator.RegisterDefaults();
var workspace = MSBuildWorkspace.Create();
var solution = await workspace.OpenSolutionAsync(solutionTargetPath);
Project projectSilaeClientShared = solution.Projects.First(x => x.Name == "ProjectX.Shared");
I cannot even get the project directly from the filepath:
string projectFilePath = "C:\Repo\Shared.shproj";
MSBuildLocator.RegisterDefaults();
MSBuildWorkspace workspace = MSBuildWorkspace.Create();
Project project = workspace.OpenProjectAsync(projectFilePath).Result;
The error message says: (Immpossible to open the project ... as the extension ".shproj" is not associated to a language)
InnerException {"Impossible d'ouvrir le projet 'C:\Repo\Shared.shproj', car l'extension de fichier '.shproj' n'est pas associée à un langage."} System.Exception {System.InvalidOperationException}
How can I get the project with Roslyn and add the modifications?
Help!
Upvotes: 0
Views: 91
Reputation: 19021
Shared projects don't quite fit in to Roslyn's model well. A Project from our perspective is not a project file on disk but more "an invocation of the compiler", and so the contents of a shared project (like source files) are then instead source files in their consuming .csproj. We don't represent .shproj special in any other way. So whereas Roslyn can absolutely give you information about a .csproj (which might consume that .shproj), we won't have any support for directly understanding a .shproj or editing it. For that you could directly use the MSBuild APIs, depending on what you're trying to achieve.
Upvotes: 2