【发布时间】:2012-01-27 16:20:02
【问题描述】:
正如我的问题 Create ISO image using PowerShell: how to save IStream to file? 中所述,在 PowerShell 中我创建了一个 IStream 对象,如下所示:
$is = (New-Object -ComObject IMAPI2FS.MsftFileSystemImage).CreateResultImage().ImageStream
此对象的 (PowerShell) 类型为 System.__ComObject。不知何故,PowerShell 知道它是一个IStream:
PS C:\> $is -is [System.Runtime.InteropServices.ComTypes.IConnectionPoint]
False
PS C:\> $is -is [System.Runtime.InteropServices.ComTypes.IStream]
True
但是,转换为这种类型失败:
PS C:\> [System.Runtime.InteropServices.ComTypes.IStream] $is
Cannot convert the "System.__ComObject" value of type "System.__ComObject" to type "System.Runtime.InteropServices.ComT
ypes.IStream".
At line:1 char:54
+ [System.Runtime.InteropServices.ComTypes.IStream] $is <<<<
+ CategoryInfo : NotSpecified: (:) [], RuntimeException
+ FullyQualifiedErrorId : RuntimeException
如何在不使用 C# 代码的情况下进行这种转换?
更新:显然,正如 x0n 的回答所说,这种转换无法正常工作。
现在,我的目标是将这个 IStream COM 对象传递给一些 C# 代码(使用 Add-Type 的同一 PowerShell 脚本的一部分),在那里它将成为 System.Runtime.InteropServices.ComTypes.IStream 类型的 .NET 对象。那可能吗?如果没有,我有什么选择?
【问题讨论】:
标签: powershell com powershell-2.0 com-interop