如何使用 roslyn 将现有项目添加到 c# 解决方案? [英] How add existing project to c# solution with roslyn?

查看:38
本文介绍了如何使用 roslyn 将现有项目添加到 c# 解决方案?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将现有项目添加到我的 c# 解决方案中.我使用了 TryApplyChanges 并且它返回 true 但它不会保存对实际解决方案的更改.我使用了 Microsoft.CodeAnalysis.* 版本 1.1.1.

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. I used Microsoft.CodeAnalysis.* version 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(当前)不支持从工作区添加/或删除项目并将它们保存回解决方案文件.我们是开源的,如果您想对此进行改进.
  2. 调用 OpenProjectAsync 并不会像您期望的那样修改解决方案……它只是用信息填充工作区.
  3. 您不是在等待 Async 调用,因此即便如此,他们可能还没有完成他们的工作,然后继续前进.
  1. MSBuildWorkspace doesn't (currently) support adding/or removing projects from the workspace and saving them back to the solution file. 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.
  3. You're not awaiting the Async calls, so even then they might have not finished their work before marching on.

第一个显然对你来说是最关键的,但其他的也应该注意.

The first is obviously the most critical for you, but the others should be noted too.

这篇关于如何使用 roslyn 将现有项目添加到 c# 解决方案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆