Reputation: 61341
Is there a way to serialize a C# structure, annotated with [StructLayout]
, into a managed byte array, i. e. a byte[]
, either premade or with allocation?
I can see marshaling to unmanaged memory then copying, but that's ugly.
Upvotes: 1
Views: 2241
Reputation: 22433
Checkout MemoryMarshal.Cast<TFrom, TTo>()
. It will easily allow you to change from byte/short/int/long arrays to Structures and back.
Upvotes: 3
Reputation: 69
From my experiences mixing managed and unmanaged data is all about clearly defining the transition from one space to another.
When i have had the requirement of going from native to managed or the other way the first step has always been to copy data to the 'target' space and then forward that.
I assume that you are already familiar with the interop services since you mentioned copying and [StructLayout]
.
If you find a better way please do tell
Upvotes: 0