Looking for a way to assign byte array to an object - tried using -as operator with no luck. (Targeting PowerShell version 5.1)
$CSharpCode = @"
using System;
namespace TestStructure
{
public struct TestStructure3
{
public Byte Field1;
public Byte Field2;
public UInt32 Field3;
public UInt32 Field4;
}
public struct TestStructure2
{
public UInt32 Field1;
public UInt32 Field2;
}
public struct TestStructure1
{
public UInt16 Field1;
public UInt16 Field2;
public TestStructure2 Field3;
public TestStructure3 Field4;
}
}
"@
Add-Type -TypeDefinition $CSharpCode
[byte[]]$BytesArray = 1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22
$MyObject = $BytesArray -as [TestStructure.TestStructure1]
- that last call fails,
$MyObjectis$null
[byte[]]$BytesArray = 1, ...works fine. Is your real question "How do I create an instance of my struct type from a binary array"?TestStructure1, then enumerate its properties:$obj = [TestStructure.TestStructure1]::new(); $obj.PSObject.Properties.ForEach{ $_ }. Assign bytes to property values based on their size.PtrToStructure().