PowerShell:如何将COM对象转换为.NET互操作类型? [英] PowerShell: how to convert a COM object to an .NET interop type?
问题描述
如我的问题使用PowerShell创建ISO映像:如何将IStream保存到文件?中所述,在PowerShell中,我创建一个< a href =http://technet.microsoft.com/en-us/query/aa380034 =nofollow> 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] $是
无法将类型System .__ ComObject的System .__ ComObject值转换为类型System.Runtime.InteropServices.ComT
ypes.IStream 。
at line:1 char:54
+ [System.Runtime.InteropServices.ComTypes.IStream] $是<<<
+ CategoryInfo:NotSpecified:(:) [],RuntimeException
+ FullyQualifiedErrorId:RuntimeException
如何在不使用C#代码的情况下进行此转换?
更新:显然,此转换无法正常运行现在,我的目标是将这个 IStream
COM对象传递给一些C#代码(一部分的C#代码)。
Add-Type
),它将成为 System.Runtime.InteropServices.ComTypes.IStream 。那可能吗? 解决方案您可以尝试传递 $ is
在类似于对象
类型的(不安全)c#方法中,尝试使用 VAR
System.Runtime.InteropServices.ComTypes.IStream
public unsafe static class MyClass
{
public static void MyMethod(object Stream)
{
var i = Stream as System.Runtime.InteropServices.ComTypes.IStream;
//做一些我喜欢i.read(...)和i.write(...)
}
}
在powershell中添加类型后:
[MyClass] :: MyMethod($ is)
As described in my question Create ISO image using PowerShell: how to save IStream to file?, in PowerShell I create an IStream
object as follows:
$is = (New-Object -ComObject IMAPI2FS.MsftFileSystemImage).CreateResultImage().ImageStream
This object is of (PowerShell) type System.__ComObject
. And somehow PowerShell knows that it is an IStream
:
PS C:\> $is -is [System.Runtime.InteropServices.ComTypes.IConnectionPoint]
False
PS C:\> $is -is [System.Runtime.InteropServices.ComTypes.IStream]
True
However, a cast to this type fails:
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
How do I make this conversion work, without using C# code?
Update: Apparently this conversion cannot be made to work, as x0n's answer says.
Now, my goal is to pass this IStream
COM object to some C# code (part of the same PowerShell script using Add-Type
), where it would become a .NET object of type System.Runtime.InteropServices.ComTypes.IStream
. Is that possible? If not, what alternatives do I have?
解决方案 You can try to pass $is
in a (unsafe) c# method like an object
type and try to handle it with a VAR
declared as System.Runtime.InteropServices.ComTypes.IStream
public unsafe static class MyClass
{
public static void MyMethod(object Stream)
{
var i = Stream as System.Runtime.InteropServices.ComTypes.IStream;
//do something with i like i.read(...) and i.write(...)
}
}
In powershell after the add-type:
[MyClass]::MyMethod($is)
这篇关于PowerShell:如何将COM对象转换为.NET互操作类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!