正如在我的问题Create ISO image using PowerShell: how to save IStream to file?所描述的,在PowerShell中创建一个对象IStream
如下:PowerShell:如何将COM对象转换为.NET互操作类型?
$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#代码,它会成为System.Runtime.InteropServices.ComTypes.IStream
类型的.NET对象(使用Add-Type
相同PowerShell脚本的一部分)。那可能吗?如果没有,我有什么替代方案?
而且工作!以前我有'MyMethod(IStream i)',然后PowerShell做到了转换为'IStream',并失败。正如你所建议的那样,让.NET/CLR来完成转换。赏金来了你的方式。 – 2012-03-22 10:46:13
乐意帮忙!有同样的问题,并尝试这种方式来处理.net中的comobject – 2012-03-22 10:50:25