Dev*_*Rob 7 powershell automation visual-studio envdte nuget
我试图在软件包管理器控制台中使用powershell来编写从解决方案中删除项目的脚本,我遇到了一个非常困难的时间.
我可以轻松地添加一个项目
PM> $dte.Solution.AddFromFile("C:\Dev\Project1.csproj")Run Code Online (Sandbox Code Playgroud)
现在我想删除一个项目,但无法获得任何工作.
我尝试了很多东西,包括:
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"."Run Code Online (Sandbox Code Playgroud)
PM> $project = [EnvDTE.Project] ($project1)
Cannot convert the "System.__ComObject" value of type
"System.__ComObject#{866311e6-c887-4143-9833-645f5b93f6f1}" to type
"EnvDTE.Project".Run Code Online (Sandbox Code Playgroud)
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.""Run Code Online (Sandbox Code Playgroud)
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'.Run Code Online (Sandbox Code Playgroud)
我尝试了其他组合,但我显然在转动轮子.我很感激任何建议.
是的,我知道我已经迟到了,但我刚刚为我们编写的内部NuGet包解决了同样的问题,我想我已经找到了如何做到这一点.
事实上,微软已经(帮助)将Delete方法保留为未实现,正如我们都发现的那样,尝试在Solution2接口上调用Remove方法会根据上下文引发一系列令人兴奋的错误!
然而,我发现直接调用SolutionClass中定义的Remove方法确实有效(尽管它被Microsoft记录为仅供内部使用.但是,嘿,当其他所有选项都用尽时......).唯一的问题是运行时绑定程序有时似乎无法解决方法重载,从而产生错误:
No overload for method 'Remove' takes 1 arguments
Run Code Online (Sandbox Code Playgroud)
所有这些意味着是时候把我们的反射蜡笔拿出来了!代码如下所示:
$removeMethod = [EnvDTE.SolutionClass].GetMethod("Remove");
$solution = $dte.Solution;
$toremove = ($solution.Projects | where ProjectName -eq "<whatever>");
$removeMethod.Invoke($solution, @($toremove));
Run Code Online (Sandbox Code Playgroud)
经过一天的各种迭代(许多与问题中的那些非常相似)和不同程度的成功(取决于我是在包管理器内执行,从安装脚本内部还是在调试器内执行),上面是我发现的最可靠.
需要注意的一点是,因为反射方法是定义的EnvDTE.SolutionClass,传递它EnvDTE._Solution或EnvDTE80.Solution2抛出Type mismatch错误,所以不幸的是你不能通过cmdlet 获取你的$solution对象Get-Interface(这通常是我的首选方法).在[EnvDTE.SolutionClass]任何可能的情况下进行演员表演显然是可取的,但我再次发现这样做有不同程度的成功.因此$solution = $dte.Solution上面略显邋..
希望这对其他人有用!
看起来是“删除”而不是“删除”。请参阅这篇MSDN 文章
Project prj = dte.Solution.Projects.Item(1);
prj.Delete();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1281 次 |
| 最近记录: |