Visual studio 如何在PowerShell中创建与COM对象不同的接口

Visual studio 如何在PowerShell中创建与COM对象不同的接口,visual-studio,powershell,com,envdte,Visual Studio,Powershell,Com,Envdte,使用COM方法,如[System.Runtime.InteropServices.Marshal]:GetActiveObject(“VisualStudio.DTE”)我可以很好地导航VisualStudioDTE对象模型。例如,从我可以得到的对象,然后,和对象。但我需要它的派生接口来调用。 我找不到获得所需接口的方法,一个简单的强制转换会导致运行时错误:无法将“System.\uu-ComObject”类型的值“System.\uu-ComObject”{5c5a0070-f396-4e37

使用COM方法,如
[System.Runtime.InteropServices.Marshal]:GetActiveObject(“VisualStudio.DTE”)
我可以很好地导航VisualStudioDTE对象模型。例如,从我可以得到的对象,然后,和对象。但我需要它的派生接口来调用。 我找不到获得所需接口的方法,一个简单的强制转换会导致运行时错误:
无法将“System.\uu-ComObject”类型的值“System.\uu-ComObject”{5c5a0070-f396-4e37-a82a-1b767e272df9}转换为“EnvDTE80.Process2”


当涉及到成员绑定时,您真的不能,至少不能以PowerShell能够记住的方式

PowerShell仅根据运行时信息运行。即使您首先在C#中强制转换它,如果
QueryInterface
为该接口返回相同的指针,那么PowerShell将看到的是它当前检测到的
IDispatch
。即使您获得的对象是来自主互操作程序集的强类型版本,PowerShell也只能看到具体类型(对于
Process2
,似乎没有这样的类型)

作为解决方法,您可以使用反射:

[EnvDTE80.Process2].InvokeMember(
    'Attach2',
    [Reflection.BindingFlags]::InvokeMethod,
    <# binder: #> $null,
    <# target: #> $process,
    <# args: #> @($myEngine))
[EnvDTE80.Process2].InvokeMember(
“附件2”,
[Reflection.BindingFlags]::InvokeMethod,
$null,
$process,
@($myEngine))
[EnvDTE80.Process2].InvokeMember(
    'Attach2',
    [Reflection.BindingFlags]::InvokeMethod,
    <# binder: #> $null,
    <# target: #> $process,
    <# args: #> @($myEngine))