简体   繁体   English

如何使用roslyn将现有项目添加到c#解决方案中?

[英]How add existing project to c# solution with roslyn?

I want to add an existing project to my c# solution.I used TryApplyChanges and it returns true but it doesn't save changes to real Solution. 我想将现有项目添加到我的C#解决方案中。我使用了TryApplyChanges,它返回true,但不会将更改保存到实际的Solution中。 I used Microsoft.CodeAnalysis.* version 1.1.1. 我使用的是Microsoft.CodeAnalysis。*版本1.1.1。

 public void AddProject (string solutionName)
 {
    MSBuildWorkspace ws=MSBuildWorkspace.Create ();
    ws.OpenSolutionAsync (solutionName);
    ws.OpenProjectAsync ("ProjectName");
    if (ws.TryApplyChanges  (ws.CurrentSolution )
    {// break point is here 
    }
 } 

There's a few problems with your code: 您的代码存在一些问题:

  1. MSBuildWorkspace doesn't (currently) support adding/or removing projects from the workspace and saving them back to the solution file. MSBuildWorkspace不(当前)支持在工作空间中添加/删除项目并将其保存回解决方案文件中。 We're open source if you want to look into improving this. 如果您想对此进行改进, 我们是开源的
  2. Calling OpenProjectAsync isn't really expected to modify the solution as you're expecting to do so...it just populates the workspace with the information. 确实不希望调用OpenProjectAsync来修改解决方案,因为您只是希望这样做。它只是将信息填充到工作区中。
  3. You're not awaiting the Async calls, so even then they might have not finished their work before marching on. 您无需等待Async调用,因此即使那样,他们也可能在继续前进之前还没有完成工作。

The first is obviously the most critical for you, but the others should be noted too. 对于您来说,第一个显然是最关键的,但其他一些也应注意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Micosoft.CodeAnalysis(roslyn)将现有的C#项目添加到解决方案文件中? - Using Micosoft.CodeAnalysis(roslyn) to add existing C# projects to a solution file? 将项目添加到不适用于roslyn的解决方案中? - add project to solution not applying with roslyn? Roslyn代码修复C#-如何在预览中禁用“文档”,“项目”,“解决方案”级别的更新 - Roslyn code fix C# - How to disable 'Document', 'Project', 'Solution' level updates in preview 如何使用 c# 在 Visual Studio 2010 中以编程方式将现有项目添加到当前解决方案 - How to add an existing project to a current solution programmatically in visual studio 2010 using c# 使用 Roslyn 将 c# 代码添加到现有 .net 核心项目 - Adding c# code to existing .net core project with Roslyn 如何使用Roslyn编译C#解决方案? - How do I compile a C# solution with Roslyn? 如何使用 C# 以编程方式在 MSBuild 解决方案中添加项目依赖项 - How to add project dependencies in a MSBuild solution programmatically with C# 如何使用Roslyn在C#项目中使用各种类型 - How to get various types used in a C# project with Roslyn 如何将cs文件添加到现有C#项目? - How do I add a cs file to an existing C# project? 如何使用Roslyn在C#中添加新的运算符 - How to add new operator in C# using Roslyn
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM