Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Visual Studio扩展重新加载项目_C#_Visual Studio_Visual Studio Extensions - Fatal编程技术网

C# Visual Studio扩展重新加载项目

C# Visual Studio扩展重新加载项目,c#,visual-studio,visual-studio-extensions,C#,Visual Studio,Visual Studio Extensions,我创建了一个简单的VisualStudio2015扩展,将一个类添加到当前项目中 Project p=(Project)ProjectsComboBox.SelectedItem; 字符串projectLocation=Path.GetDirectoryName(p.FileName); //我加载要添加文件的项目 var projectCurrent=new Microsoft.Build.Evaluation.Project(p.FileName); 字符串relativeNewPath=“

我创建了一个简单的VisualStudio2015扩展,将一个类添加到当前项目中

Project p=(Project)ProjectsComboBox.SelectedItem;
字符串projectLocation=Path.GetDirectoryName(p.FileName);
//我加载要添加文件的项目
var projectCurrent=new Microsoft.Build.Evaluation.Project(p.FileName);
字符串relativeNewPath=“\\Model\\DAL\\”+ViewNameTexBox.Text+“DAO.cs”;
字符串newPath=项目位置+相对路径;
projectCurrent.AddItem(“编译”,相对路径);
projectCurrent.Save();
//一旦类添加到我的项目中,我就在新创建的类中编辑文本
string text=File.ReadAllText(“Templates/DAO.cs.txt”);
text=text.Replace(“$viewName$”,ViewNameTexBox.text);
File.writealText(newPath,text);
该文件包含在项目中并已修改,但VisualStudio会提示一条消息,要求重新加载项目。当我这样做时,项目将不会重新加载

项目已在外部环境中修改

我想处理当前项目,但项目不可IDisposable。将currentProject设置为null无效


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

您可以在将文件添加到project之前卸载该项目,然后再次加载project,以下代码供您参考

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);

为什么要添加标记?@IanAbbott miss click autocomplete,将其更改为c#我找到了另一种方法,请参阅我的答案使用此解决方案我在项目根目录下添加了项目链接文件,但实际文件位于正确的位置(\\Model\\DAO)。它还提供重新加载项目,但这次我可以重新加载。找到了!relativePath必须为“Model\\DAL”而不是“\\Model\\DAL”