Addin Development: Add MSBuild Import to your csproj without unload and reloading the project using DTE
Yesterday I started developing an Addin which have to add an Import clause to the selected Project. After asking Google I thought that I have to unload the Project, modify the csproj file (XML or Microsoft.Build) and after that reload the Project using DTE.
I think this is no good Option because if you have in-memory changes at the Project file it will be saved and you don't have the possibility for undoing the previouse changes or the adding of the Targets file.
So today I found the solution:
You have to use the "Microsoft.Build.Evaluation" Namespace.
Adding a import clause to the in memory loaded csproj:
foreach(var prj in Microsoft.Build.Evaluation.ProjectCollection.GlobalProjectCollection.LoadedProjects)
{
if (string.Compare(prj.FullPath, dteProject.FullName) == 0)
{
prj.Xml.AddImport(@"YourDesiredTargetFile.targets");
prj.MarkDirty();
}
}
All done ;)
Kommentare
Kommentar veröffentlichen