如我在问题描述中所述使用PowerShell创建ISO镜像:如何将IStream保存到文件?,在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对象。这是可能的吗?如果不行,我有哪些替代方案?
MyMethod(IStream i)
,然后 PowerShell 尝试将其转换为IStream
时失败了。按照您的建议,让 .NET/CLR 来进行转换就可以了。奖金即将到账。 - MarnixKlooster ReinstateMonica