2023-08-23 17:16:13 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
#pragma warning disable CS8600
|
|
|
|
|
#pragma warning disable CS8603
|
|
|
|
|
|
|
|
|
|
namespace service
|
|
|
|
|
{
|
|
|
|
|
public class Helper
|
|
|
|
|
{
|
2023-09-02 18:55:40 +02:00
|
|
|
|
unsafe public static T BytesToStructure<T>(ref byte[] buffer, int offset)
|
2023-08-23 17:16:13 +02:00
|
|
|
|
{
|
|
|
|
|
int typeSize = Marshal.SizeOf(typeof(T));
|
|
|
|
|
IntPtr ptr = Marshal.AllocHGlobal(typeSize);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-09-02 18:55:40 +02:00
|
|
|
|
Marshal.Copy(buffer, offset, ptr, typeSize);
|
2023-08-23 17:16:13 +02:00
|
|
|
|
return (T)Marshal.PtrToStructure(ptr, typeof(T));
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
Marshal.FreeHGlobal(ptr);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#pragma warning restore CS8600
|
|
|
|
|
#pragma warning restore CS8603
|