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

Can't access FileCodeModel from Powershell console in Visual Studio 2017 DTE

我正在尝试使用集成的 Powershell 控制台和 VS 自动化模型 (DTE) 在 VisualStudio 2017 中自动化一些与代码相关的例程。当我在 Solution/Project/File 级别工作时,一切正常,例如

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".

试试这个:

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