无法从Visual Studio 2017 DTE中的Powershell控制台访问FileCodeModel

时间:2017-07-21 12:07:43

标签: visual-studio powershell com envdte

我尝试使用集成的Powershell控制台和VS自动化模型(DTE)在VisualStudio 2017中自动执行一些与代码相关的例程。当我在解决方案/项目/文件级别上工作时,情况还可以,例如。

PS> $dte.ActiveDocument.ProjectItem

IsDirty              : False
FileCount            : 1
Name                 : FeaturesComposition.cs
Collection           : System.__ComObject
Properties           : System.__ComObject
DTE                  : System.__ComObject
Kind                 : {6BB5F8EE-4483-11D3-8BCF-00C04F8EC28C}
ProjectItems         : System.__ComObject
Object               : System.__ComObject
ExtenderNames        : {}
ExtenderCATID        : {610D4615-D0D5-11D2-8599-006097C68E81}
Saved                : True
ConfigurationManager : 
FileCodeModel        : System.__ComObject
Document             : System.__ComObject
SubProject           : 
ContainingProject    : System.__ComObject

但是当我来到某个特定文件的代码模型时,没有什么可以解决的:

PS> $dte.ActiveDocument.ProjectItem.FileCodeModel | Format-List -Property *
System.__ComObject

PS> $dte.ActiveDocument.ProjectItem.FileCodeModel | gm

PS> 

是否可以访问此类子模型?有没有简单的方法将EnvDTE.DTE接口分配给现有的$ dte实例?我在下面尝试了一些想法,但没有成功。

Add-Type -Path "$env:VSAPPIDDIR\PublicAssemblies\envdte.dll"

PS> # Explicit cast doesn't work
PS> [EnvDTE.DTE]$dte

[ERROR] Cannot convert the "System.__ComObject" value of type "System.__ComObject#{04a72314-32e9-48e2-9b87-a63603454f3e}" to type "EnvDTE.DTE".

PS> # Wrapper works but it's useless
PS> $wrapped = [Runtime.InteropServices.Marshal]::CreateWrapperOfType($dte, [EnvDTE.DTEClass])
PS> $wrapped.ActiveDocument.ProjectItem.FileCodeModel

System.__ComObject

PS> # GetComInterfaceForObject gives the same IntPtr as IUnknown:QueryInterface
PS> # different from the call to GetComInterfaceForObject for example,
PS> # so I hoped to get another casting results. But it is the same.
PS> $contract = [Runtime.InteropServices.Marshal]::GetComInterfaceForObject($dte, [EnvDTE.DTE])
PS> [EnvDTE.DTE][Runtime.InteropServices.Marshal]::GetObjectForIUnknown($contract)

[ERROR] Cannot convert the "System.__ComObject" value of type "System.__ComObject#{04a72314-32e9-48e2-9b87-a63603454f3e}" to type "EnvDTE.DTE".

1 个答案:

答案 0 :(得分:1)

尝试一下:

 $fileCodeModel = Get-Interface $dte.ActiveDocument.ProjectItem.FileCodeModel ([ENVDTE80.FileCodeModel2])