Initial commit
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace RobotNet10.ScriptEditor.Helpers.Webcil;
|
||||
|
||||
internal static class StreamExtensions
|
||||
{
|
||||
internal static void WriteStruct<T>(this Stream stream, T structData) where T : struct
|
||||
{
|
||||
var bytes = StructToBytes(structData);
|
||||
stream.Write(bytes);
|
||||
}
|
||||
|
||||
private static byte[] StructToBytes<T>(T structData) where T : struct
|
||||
{
|
||||
int size = Marshal.SizeOf(structData);
|
||||
byte[] byteArray = new byte[size];
|
||||
nint ptr = Marshal.AllocHGlobal(size);
|
||||
|
||||
try
|
||||
{
|
||||
Marshal.StructureToPtr(structData, ptr, false);
|
||||
Marshal.Copy(ptr, byteArray, 0, size);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Marshal.FreeHGlobal(ptr);
|
||||
}
|
||||
|
||||
return byteArray;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user