我有一些使用 COM API 的 PowerShell 代码。传入字节数组时出现类型不匹配错误。这是我创建数组的方式,以及一些类型信息
PS C:\> $bytes = Get-Content $file -Encoding byte
PS C:\> $bytes.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
PS C:\> $bytes[0].GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Byte System.ValueType
仔细研究 API,我发现它正在寻找一个基本类型为 System.Array 的 Byte[]。
PS C:\> $r.data.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Byte[] System.Array
PS C:\> $r.data[0].gettype()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Byte System.ValueType
我要做的是将$bytes 转换为与$r.data 相同的类型。出于某种原因,$bytes 被创建为 Object[]。如何将其转换为 Byte[]?