[英]Visual Studio Extension Reload Project
我創建了一個簡單的Visual Studio 2015擴展,將一個類添加到當前項目中。
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會提示消息,要求重新加載項目。 當我這樣做時,該項目將不會重新加載。
該項目已在環境外部進行了修改
我想處置currentProject但項目不是IDisposable。 將currentProject設置為null無效。
如何告訴我的代碼釋放currentProject並讓Visual Studio重新加載它?
您可以先卸載項目,然后再將文件添加到項目中,然后再次加載項目,以下代碼供您參考。
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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.