Visual Studio 扩展重新加载项目 [英] Visual Studio Extension Reload Project

查看:28
本文介绍了Visual Studio 扩展重新加载项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个简单的 Visual Studio 2015 扩展,用于向当前项目添加一个类.

I created a simple visual studio 2015 extension that adds a class to the current project.

Project p = (Project) ProjectsComboBox.SelectedItem;

string projectLocation = Path.GetDirectoryName(p.FileName);
// I load the project where I want to Add file
var projectCurrent = new Microsoft.Build.Evaluation.Project(p.FileName);


string relativeNewPath = "\\Model\\DAL\\" + ViewNameTexBox.Text + "DAO.cs";
string newPath = projectLocation + relativeNewPath;

projectCurrent.AddItem("Compile", relativeNewPath);
projectCurrent.Save();

// Once class added to my project I edit text inside newly create class
string text = File.ReadAllText("Templates/DAO.cs.txt");
text = text.Replace("$viewName$", ViewNameTexBox.Text);
File.WriteAllText(newPath, text);

该文件已包含在项目中并已修改,但 Visual Studio 提示一条消息,要求重新加载项目.当我这样做时,项目将不会重新加载.

The file is included to the project and modified but visual studio prompt a message asking to reload project. When I do so, the project won't reload.

项目已在环境外修改

我想处理 currentProject 但项目不是 IDisposable.将 currentProject 设置为 null 无效.

I would like to dispose currentProject but project is not IDisposable. Setting currentProject to null has no effect.

如何告诉我的代码释放 currentProject 并让 Visual Studio 重新加载它?

How can I tell my code to free currentProject and let Visual Studio reload it ?

推荐答案

可以先卸载项目,再向项目添加文件,然后再加载项目,以下代码供大家参考.

You can unload the project before add files to project, then load project again, The following code for your reference.

Microsoft.Build.Evaluation.ProjectCollection projectCollection = new Microsoft.Build.Evaluation.ProjectCollection();

var projectCurrent = projectCollection.LoadProject(projectPath);
projectCollection.UnloadProject(projectCurrent);

string relativeNewPath = "\\Model\\DAL\\DAO.cs";
string newPath = projectPath + relativeNewPath;
projectCurrent.AddItem("Compile", relativeNewPath);
projectCurrent.Save();
projectCollection.LoadProject(projectPath);

这篇关于Visual Studio 扩展重新加载项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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