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

问题描述 投票:3回答:1

我正在尝试使用集成的Powershell控制台和VS自动化模型(DTE)在Visual Studio 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".
visual-studio powershell com envdte
1个回答
0
投票

尝试一下:

 $fileCodeModel = Get-Interface $dte.ActiveDocument.ProjectItem.FileCodeModel ([ENVDTE80.FileCodeModel2])
© www.soinside.com 2019 - 2024. All rights reserved.