Delete a project from the solution through the package manager console

I am trying to use powershell in the package manager console for a script to remove a project from a solution, and I have an amazingly difficult time.

I can easily add a project

PM> $dte.Solution.AddFromFile("C:\Dev\Project1.csproj") 

Now I want to delete the project and cannot make anything work.

I have tried several things, including:

 PM> $project1 = Get-Project "Project1Name" PM> $dte.Solution.Remove($project1)  PM> $project1 = Get-Project "Project1Name" PM> $dte.Solution.Remove($project1) >

 Cannot convert argument "0", with value: "System .__ ComObject", for "Remove" to
 type "EnvDTE.Project": "Cannot convert the" System .__ ComObject "value of type
 "System .__ ComObject # {866311e6-c887-4143-9833-645f5b93f6f1}" to type
 "EnvDTE.Project". " 
 PM> $project = Get-Interface $project1 ([EnvDTE.Project]) PM> $dte.Solution.Remove($project) Cannot convert argument "0", with value: "System.__ComObject", for "Remove" to type "EnvDTE.Project": "Cannot convert the "System.__ComObject" value of type "NuGetConsole.Host.PowerShell.Implementation.PSTypeWrapper" to type "EnvDTE.Project"." 
 PM> $project = [EnvDTE.Project] ($project1) Cannot convert the "System.__ComObject" value of type "System.__ComObject#{866311e6-c887-4143-9833-645f5b93f6f1}" to type "EnvDTE.Project". 
 PM> $solution2 = Get-Interface $dte.Solution ([EnvDTE80.Solution2]) PM> $solution2.Remove($project1) Exception calling "Remove" with "1" argument(s): "Exception calling "InvokeMethod" with "3" argument(s): "Object must implement IConvertible."" 
 PM> $dte2 = Get-Interface $dte ([EnvDTE80.DTE2]) PM> $dte2.Solution.Remove($project) Method invocation failed because [System.Object[]] doesn't contain a method named 'Remove'. 

I tried other combinations, but I am clearly spinning my wheels. I appreciate any suggestions.

+6
source share
2 answers

That's right, I know I'm late for the party, but I just addressed the same issue for the internal NuGet package we are writing, and I think I found how to do it.

Indeed, Microsoft (using) left the Delete unimplemented method, and, as we both discovered, an attempt to call Remove on the Solution2 interface produces an exciting array of errors depending on the context!

However, I found that I directly called the Remove method defined in SolutionClass really works (despite the fact that Microsoft is only documented as internal use. But when every other option has been exhausted ...). The only catch is that the run-time middleware sometimes also cannot resolve the method overload, creating an error:

 No overload for method 'Remove' takes 1 arguments 

All this means it's time to get our reflective pencils! The code is as follows:

 $removeMethod = [EnvDTE.SolutionClass].GetMethod("Remove"); $solution = $dte.Solution; $toremove = ($solution.Projects | where ProjectName -eq "<whatever>"); $removeMethod.Invoke($solution, @($toremove)); 

After a day of various iterations (many of which are very similar to questions) and with varying degrees of success (depending on whether I was running inside the package manager, inside the script installation or inside the debugger), the above is what I found the most reliable.

It should be noted that since the reflected method is defined in EnvDTE.SolutionClass , passing it to EnvDTE._Solution or EnvDTE80.Solution2 throws a Type mismatch error, so, unfortunately, you cannot get your $solution object using Get-Interface (which is usually mine preferred method). Obviously, casting to [EnvDTE.SolutionClass] , if possible, preferable, but again I found a variable degree of success in this. Therefore, a bit sloppy $solution = $dte.Solution higher.

Hope this is helpful to someone else!

+4
source

It appears that instead of β€œDelete” it is β€œDelete”. See MSDN Article

 Project prj = dte.Solution.Projects.Item(1); prj.Delete(); 
+1
source

Source: https://habr.com/ru/post/949913/


All Articles